How to get an input from keyboard using Java
Posted: Mon Jul 04, 2011 1:12 pm
While there are many methods to do that, I will show two methods and I prefer the second method to first one.
Method 1:
Method 2:
One of the things that I like in Java is Exception Handling (just like in C++). The programmer can identify the error whenever it occurs and handle it as needed. In legacy languages such as C, program terminates itself when it reaches an error. Here is the code with Exception Handling in Java (Don;t think the code has become too complicated with it).
Method 2 with exception handling:
Here is program with which reads integer inputs such as age.
Likewise, the input is stored in a string so if you want to use the input as anything other than a string it needs to be converted into the right variable type. Java provides a way to do this as shown below.
Note that x, y, etc. must first been declared as their respective variable types before you assign the result of the string conversion.
Method 1:
Code: Select all
java.io.DataInputStream in = new java.io.DataInputStream(System.in);
String aLine = in.readLine();
Code: Select all
System.out.println("Please enter your telephone number");
String strPhone = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
strPhone = br.readLine();
Method 2 with exception handling:
Code: Select all
System.out.println("Please enter your telephone number");
String strPhone = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
strPhone = br.readLine();
}
catch (IOException err) {
System.out.println("Error reading line");
}
Code: Select all
import java.io.* ;
class Example1 {
public static void main(String args[])
{
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("Welcome To Java Input");
try {
System.out.println("Please Enter In Your First Name: ");
String firstName = bufRead.readLine();
System.out.println("Please Enter In The Year You Were Born: ");
String bornYear = bufRead.readLine();
System.out.println("Please Enter In The Current Year: ");
String thisYear = bufRead.readLine();
int bYear = Integer.parseInt(bornYear);
int tYear = Integer.parseInt(thisYear);
int age = tYear – bYear ;
System.out.println("Hello " + firstName + " You are " + age + " years old");
}
catch (IOException err) {
System.out.println("Error reading line");
}
catch(NumberFormatException err) {
System.out.println("Error Converting Number");
}
}
}
Code: Select all
x = Double.parseDouble(temp); // string temp converted to double x
y = Float.parseFloat(temp); // string temp converted to float y
i = Long.parseLong(temp); // string temp converted to long i
j = Integer.parseInt(temp); // string temp converted to int j
k = Short.parseShort(temp); // string temp converted to short k
m = Byte.parseByte(temp); // string temp converted to byte m
a = Boolean.valueOf(temp).booleanValue(); // string temp converted to boolean a