Java Input
We know that taking data inputs are important parts of a computer program. Sometimes our application will need to read input data from keyboard. Java provides classes in the packages such as java.lang, java.io and java.util to facilitate accepting data inputs. Several techniques are discussed here.
1. Using Command Line Arguments
We can take keyboard input using command line arguments. In the given addition example, we are expecting two input values.
The first input is denoted as “args[0]” and the first input is denoted as “args[1]“. Since, both of them are String values; we need to convert them into integer values for calculating the sum.
The Double class belongs to the java.lang package; so, we don’t have to import anything. See the code below.
CommandLine.java
public class CommandLine {
public static void main(String[] args) {
int n = args.length;
int sum;
if (n < 2) {
System.out.println("INPUT ERROR!");
System.exit(0);
}
else {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
sum = a + b;
System.out.println("SUM = " + sum);
}
}
}
Output:
C:\testing>java CommandLine 20 30
SUM = 50
2. Using System class
We can also accept keyboard input using the java.lang.System class. It is a very useful technique when we don’t have support for packages like java.io or java.util.
Here, we are taking as input one character value (stored in variable in ch) at a time. Subsequently, we are appending this character value to the string named “str“.
When we press ‘@‘ symbol, the input taking is stopped. If everything goes fine, the string value will be converted to a decimal number and finally be displayed.
As System.in.read() method has an exception associated with it, we have declared Exception class in the main() method signature.
Since the classes System and Double belong to the java.lang package, we don’t have to import anything. See the program below.
SimpleInput.java
public class SimpleInput {
public static void main(String[] args) throws Exception {
char ch;
String str = "";
System.out.println("Enter a decimal number (press '@' to quit): ");
while( (ch=(char)System.in.read()) != '@') {
str += ch;
}
double num = Double.parseDouble(str);
System.out.println("Number = " + num);
}
}
Output:
C:\testing>java SimpleInput
Enter a decimal number (press '@' to quit):
1234.75@
Number = 1234.75
3. Using BufferedReader class
We may also use java.io.BufferedReader class for keyboard input. Within the object of BufferedReader class, we have to wrap the object of java.io.InputStreamReader class; as because this class has a direct connection with the keyboard (i.e System.in).
This technique was very popular in Java 2, before the introduction of Scanner class.
As the readLine() method of BufferedReader has an exception (java.io.IOException) associated with it, we have to declare this exception in the main() method signature. See it below.
Java2Input.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Java2Input {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter two decimal numbers: ");
double a = Double.parseDouble(input.readLine());
double b = Double.parseDouble(input.readLine());
double sum = a + b;
System.out.println("SUM = " + sum);
}
}
Output:
C:\testing>java ConsoleTest
Enter two decimal numbers:
24.35
33.40
SUM = 57.75
4. Using Scanner class
We can take keyboard input using java.util.Scanner class. This technique is very popular among the programmers since its introduction with Java SE 5. See the program below.
ScannerTest.java
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
// Creating object
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer: ");
int i = sc.nextInt();
System.out.println("Enter a word: ");
String s = sc.next();
System.out.println("Enter a boolean: ");
boolean b = sc.nextBoolean();
System.out.println("Enter a sentence: ");
// Creating a new object of Scanner is a must for a sentence
String str = new Scanner(System.in).nextLine();
System.out.println("Enter a decimal no: ");
double d = sc.nextDouble();
System.out.println("\nDisplay values: ");
System.out.println("i = " + i);
System.out.println("s = " + s);
System.out.println("b = " + b);
System.out.println("str = " + str);
System.out.println("d = " + d);
}
}
Output:
C:\testing>java ScannerTest
Enter an integer:
12
Enter a word:
Delhi
Enter a boolean:
true
Enter a sentence:
Java programming is a fun!
Enter a decimal no:
23.75
Display values:
i = 12
s = Delhi
b = true
str = Java programming is a fun!
d = 23.75
NOTE: With this technique, creating a new object of Scanner is a must for inputting a sentence. Otherwise, that input taking task will be skipped to the next one.
5. Using Console class
We may accept keyboard input using java.io.Console class. This class has been introduced in Java since JSE 6.
Here, we use the console() method of System class to create an object of Console class. See it below.
ConsoleTest.java
import java.io.Console;
public class ConsoleTest {
public static void main(String[] args) {
Console cs = System.console();
System.out.println("Enter two decimal numbers: ");
double a = Double.parseDouble(cs.readLine());
double b = Double.parseDouble(cs.readLine());
double sum = a + b;
System.out.println("SUM = " + sum);
}
}
Output:
C:\testing>java ConsoleTest
Enter two decimal numbers:
24.35
33.40
SUM = 57.75
NOTE: This technique will not work with the IDEs like NetBeans or Eclipse. It can be used in the console only.