How to get an input from keyboard using Java

Java programming topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to get an input from keyboard using Java

Post by Saman » 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:

Code: Select all

java.io.DataInputStream in = new java.io.DataInputStream(System.in);
String aLine = in.readLine();
Method 2:

Code: Select all

System.out.println("Please enter your telephone number");  
String strPhone = "";  
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
strPhone = br.readLine();  
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:

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");
}
Here is program with which reads integer inputs such as age.

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");
          }
     }
}
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.

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
Note that x, y, etc. must first been declared as their respective variable types before you assign the result of the string conversion.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: How to get an input from keyboard using Java

Post by Nipuna » Tue Jul 05, 2011 8:07 pm

Thanks Great Explanation :)
Post Reply

Return to “Java Programming”