1. Using BufferedReader
==> Without the try-catch block
a. Input Data String
public static void main (String [] args) throws IOException {
InputStreamReader reader = new InputStreamReader (System.in);
BufferedReader input = new BufferedReader (reader);
System.out.print ("Enter Any Word =");
String text = input.readLine ();
b. Input Data Characters
public static void main (String [] args) throws IOException {
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("Enter Any Word =");
char ch = (char) br.read () ;/ / for character
/ * Can also tipedata first declaration, that is
new char ch then ch = (char) br.read () * /
==> Without the try-catch block
a. Input Data String
public static void main (String [] args) throws IOException {
InputStreamReader reader = new InputStreamReader (System.in);
BufferedReader input = new BufferedReader (reader);
System.out.print ("Enter Any Word =");
String text = input.readLine ();
b. Input Data Characters
public static void main (String [] args) throws IOException {
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("Enter Any Word =");
char ch = (char) br.read () ;/ / for character
/ * Can also tipedata first declaration, that is
new char ch then ch = (char) br.read () * /
/ * The following statement is the same
A. InputStreamReader reader = new InputStreamReader (System.in);
BufferedReader input = new BufferedReader (reader);
-> Reader = new InputStreamReader (System.in) <-
2. BufferedReader input = new BufferedReader
(New InputStreamReader (System.in));
==> With the try-catch block
BufferedReader dataInput = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("Enter Any Word =");
String text = "" ;/ / String variable declaration
try {
dataInput.readLine = text () ;/ / read a string
} Catch (IOException e) {/ / e is the name of the identifier (ID)
System.out.println ("Error!");
}
note:
in the use of a try-catch statement then be without or throws Exception throws IOException
c. Numerical Data Input
public static void main (String [] args) throws IOException {
System.out.print ("Enter an integer:");
String temp;
int number = 0;
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
/ / Input data is considered as a string
temp = br.readLine ();
try {
/ / Conversion from string to integer
number = Integer.parseInt (temp);
} Catch (NumberFormatException NFE) {
System.out.println ("Input data" +
"Is not an integer");
System.exit (1);
}
/ * The above error detection can occur due to eg a computer network is disconnected,
there is an error in the system hardware, etc. * /
2.using JOptionPane
public static void main (String [] args)
{
String text = JOptionPane.showInputDialog ("Enter Data");
System.out.print ("The data entered is =" + text);
System.exit (0);
}
source: STMIK AMIKOM YOGYAKARTA
A. InputStreamReader reader = new InputStreamReader (System.in);
BufferedReader input = new BufferedReader (reader);
-> Reader = new InputStreamReader (System.in) <-
2. BufferedReader input = new BufferedReader
(New InputStreamReader (System.in));
==> With the try-catch block
BufferedReader dataInput = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("Enter Any Word =");
String text = "" ;/ / String variable declaration
try {
dataInput.readLine = text () ;/ / read a string
} Catch (IOException e) {/ / e is the name of the identifier (ID)
System.out.println ("Error!");
}
note:
in the use of a try-catch statement then be without or throws Exception throws IOException
c. Numerical Data Input
public static void main (String [] args) throws IOException {
System.out.print ("Enter an integer:");
String temp;
int number = 0;
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
/ / Input data is considered as a string
temp = br.readLine ();
try {
/ / Conversion from string to integer
number = Integer.parseInt (temp);
} Catch (NumberFormatException NFE) {
System.out.println ("Input data" +
"Is not an integer");
System.exit (1);
}
/ * The above error detection can occur due to eg a computer network is disconnected,
there is an error in the system hardware, etc. * /
2.using JOptionPane
public static void main (String [] args)
{
String text = JOptionPane.showInputDialog ("Enter Data");
System.out.print ("The data entered is =" + text);
System.exit (0);
}
source: STMIK AMIKOM YOGYAKARTA
0 komentar
Posting Komentar