Java tutorial- read input values from keyboard

Read input values from keyboard

Reading single character 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.


public class Getcha
   public static void main(String[] args) throws Exception
      char c;
      System.out.print("Enter a character:");
      c = (char);
      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.


import*; // 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.out.print(“Enter your name:”);


     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 ().


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.


import*; // 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.out.print(“Enter your favorite number:”);


      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.


import java.util.Scanner;

public class ScannerInput {

public static void main(String[] args) {

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




Laxman comment


Why gives that much options for reading value from keyboard? on one is enough na

Merit Campus comment

 Merit Campus

You can clear many of your doubts in Core Java through Merit Campus, visit:

We also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: to know more.

Dara comment


What did the error you get?

Joshua comment


I can't get the readLine example to compile...

Dara Chan comment

 Dara Chan

It is a useful tutorial. I need it.

Dara comment


You can get example Java programs from the links: and

meghana comment


Give me somewhat simple program please.

Tarequzzaman comment


But this process takes a long time for run;

Amarjeet Deol comment

 Amarjeet Deol

Good job , pls help us to learn core java and advance java.

Dara comment


You can set the number of digits to display after the decimal point by using the applyPattern method of the DecimalFormat class. See the example code below.
NumberFormat nf=NumberFormat.getInstance();
DecimalFormat df=(DecimalFormat)nf;
double num=8973.9887;
String mynum=df.format(num);

Syalys comment


Hello all, i have a number format in Java problem. I tries to use the NumberFormat class to format a number. For example, by running the code the number 987999.9987 is formatted as 987,999.9987. But the result i got is 987,999.99. How can i achieve this goal?

PRAKASH comment


Thanks Brother..... I searched this kind of coding in so many places.....

noob comment


how come there is no tutorial on scanner


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.