Read input values from keyboard

Reading single character

System.in.read() method is used to read a single character from user’s input value. It will return numerical value of the input character. Therefore, you need to convert the value to the character using (char) conversion statement otherwise you may not understand what the user input.

Example:

public class Getcha
{
   public static void main(String[] args) throws Exception
   {
      char c;
      System.out.print("Enter a character:");
      c = (char)System.in.read();
      System.out.println("You entered " + c);
   }

}

In the sample above, exception must be thrown to pass the errors to the operating system to handle them. If exception is not thrown, your program will not be compiled.

 

Reading strings

In Java, one way to get a string value from keyboard is using the readLine() method of BufferedReader class. To use this method, you must create BufferedReader object. See the example code below.

Example:

import java.io.*; // import IO package

public class ReadString
{
   public static void main(String[] args)throws IOException
   {
      String yourname;

      BufferedReader reader;//create BufferedReader object

      reader=new BufferedReader(new InputStreamReader(System.in));

      System.out.print(“Enter your name:”);

      yourname=reader.readLine(); 

     System.out.println(“Your name is ”+yourname);
        }
}

 

Type conversions

In Java programming language, you can convert from one type of data to another type by specifying that type in brackets ().

Example:

public class Conversion
{
   public static void main(String[] args)
   {
      int i;
      char c = 'a';
      i = (int)c; // The variable c with data type char is converted to integer type by using (int).
      i = 10;
      c = (char)i;
   }
}

In some circumstances, you need to convert a string value to an integer value. Because String  is not a simple data type but it is a class type, you cannot convert it as in the above example. Instead, you need to use Interger.parseInt() method of Integer class.

Example:

import java.io.*; // import IO package

public class ConvertString
{
   public static void main(String[] args)throws IOException
   {
      String valString;

      int val;

      BufferedReader reader;//create BufferedReader object

      reader=new BufferedReader(new InputStreamReader(System.in));

      System.out.print(“Enter your favorite number:”);

      valString=reader.readLine(); 

      val=Integer.parseInt(valString); //string value is converted to integer value

      System.out.println(“Your favorite number is ”+val);
        }
}

Scanner class

Scanner is a popular class used to get input from keyboard. This class has different methods to read different types of data. Below are the frequently used methods of the Scanner class.

-next() reads a string value. If the string value value contains white spaces, only the first part of the string is read.

-nextLine() reads a string value.

-nextInt() reads an integer number.

-nextFloat() reads a float number.

-nextDouble() reads a double number.

-nextBool() reads a boolean value.

-nextByte() reads a byte number.

Example:

import java.util.Scanner;

public class ScannerInput {

public static void main(String[] args) {

int qty;
float price;
float amount;
Scanner sc=new Scanner(System.in);
System.out.println("Enter quantity:");
qty=sc.nextInt();
System.out.println("Enter unit price:");
price=sc.nextFloat();
amount=qty*price;
System.out.println("The total sale is "+amount);
sc.close();

}

}



HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.