MENDAPATKAN INPUT DARI KEYBOARD
Tujuan Membuat program kita lebih interaktif dengan menggunakan input dari keyboard Menggunakan kelas BufferedReader dan melalui GUI (Graphical User Interface) dengan menggunakan kelas JOptionPane.
Menggunakan BufferedReader Tambahkan di bagian paling atas code anda: import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; Tambahkan dibawah viod main : BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
Contoh package inputkey; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main{ public static void main( String[] args ){ BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) ); String name = ""; System.out.print("Please Enter Your Name:"); try { name = dataIn.readLine();} catch( IOException e ){ System.out.println( " Error ");} System.out.println("Hello " + name + " ! "); }}
package inputkey; import java.io.*; public class Main { public static void main( String[] args ) BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) ); String name = ""; System.out.print("Please Enter Your Name:"); try name = dataIn.readLine(); } catch( IOException e ) System.out.println( " Error "); System.out.println("Hello " + name + " ! ");
Menggunakan Scanner Tambahkan di bagian paling atas code anda: import java.util.*; Tambahkan dibawah viod main : Scanner baca = new Scanner(System.in);
package satu; import java.util.*; public class Main { public static void main(String[] args) Scanner baca = new Scanner(System.in); int nilai; System.out.print(“Input Anda = "); nilai = baca.nextInt(); System.out.println("Nilai Anda = " +nilai); }
Menggunakan JOptionPane Tambahkan di bagian paling atas code anda: import javax.swing.JOptionPane; Tambahkan dibawah viod main : name = JOptionPane.showInputDialog("Please enter your name"); String msg = "Hello " + name + "!"; JOptionPane.showMessageDialog(null, msg);
package jopt; import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String name = ""; name = JOptionPane.showInputDialog("Please enter your name"); String msg = "Hello " + name + "!"; JOptionPane.showMessageDialog(null, msg); }
Latihan A = B = C = A * B Buat dengan BufferedReaderdan Joption Pane