Java tutorial- read input values from keyboard

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

}

}





Comments

Dara comment

 Dara

You can get example Java programs from the links: http://www.worldbestlearningcenter.com/index_files/home-ass.htm and http://java.worldbestlearningcenter.com/search/label/program.

09-11-2014
meghana comment

 meghana

Give me somewhat simple program please.

09-10-2014
Tarequzzaman comment

 Tarequzzaman

But this process takes a long time for run;

06-09-2014
Amarjeet Deol comment

 Amarjeet Deol

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

03-25-2014
Dara comment

 Dara

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;
df.applyPattern("#,###.00");
double num=8973.9887;
String mynum=df.format(num);
System.out.println(mynum);

03-16-2014
Syalys comment

 Syalys

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?

03-16-2014
PRAKASH comment

 PRAKASH

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

01-13-2014
noob comment

 noob

how come there is no tutorial on scanner

08-28-2013
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
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.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like