In Java, there are many ways to read strings from input. The following are some ways to read data from the keyboard.
- Scanner Class
- InputStreamReader and BufferedReader class
- Console (I/O) class
- DataInputStream class.
Scanner class
The Scanner class is used to get user input, and it is found in java.util package. Scanner class is the most common way to take user input in Java.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class.
Some common methods are:
Method | Description |
nextInt() | Used for reading int value. |
nextLong() | Used for reading long value. |
nextShort() | Used for reading short value. |
nextFloat() | Used for reading float value. |
nextDouble() | Used for reading double value. |
next() | Used for reading string(word) value |
nextLine() | Used for reading String (Sentence) value |
next().CharAt(0) | |
nextByte() | Used for reading byte value. |
nextBoolean() | Used for reading boolean value. |
Example:
import java.util.Scanner;
public class inputscan {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter your name: “);
String name = sc.nextLine();
System.out.print(“Enter your age: “);
int age = sc.nextInt();
System.out.print(“Enter your Salary: “);
double sal = sc.nextDouble();
System.out.print(“Enter your address: “);
String addr = sc.next();
System.out.println(“Name:” + name);
System.out.println(“Age:” + age);
System.out.println(“Salary:” + sal);
System.out.println(“Address:” + addr);
}
}
Output:
Enter your name: Amit Kumar
Enter your age: 30
Enter your Salary: 40000
Enter your address: Tinsukia
Name:Amit Kumar
Age:30
Salary:40000.0
Address:Tinsukia
InputStreamReader class
The InputStreamReader class is used to read (or get) data from the keyboard. It performs the following two tasks:
- provide a connection to input stream of keyboard.
- converts the byte-oriented stream into character-oriented stream.
BufferedReader class
It is a simple class that is used to read a sequence of characters. It provides several methods, including:
- read(): Reads a single character.
- readLine(): Reads an entire line of text.
Example:
import java.io.*;
public class input {
public static void main(String[] args) throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.print(“Whats your name: “);
String name=br.readLine();
System.out.println(“Welcome “+name);
}
}
Output:
Whats your name: Eliza
Welcome Eliza
Another Example of reading data from keyboard by InputStreamReader and BufferdReader class until the user writes stop
In this example, we are reading and printing the data until the user prints stop.
import java.io.*;
public class inputdata {
public static void main(String[] args)throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name=””;
while(!name.equals(“none”)){
System.out.print(“Enter name: “);
name=br.readLine();
System.out.println(“Name: “+name);
}
br.close();
r.close();
}
}
Output:
Enter name: Eliza
Name: Eliza
Enter name: Sumona
Name: Sumona
Enter name: Manisha
Name: Manisha
Enter name: Jyotirupa
Name: Jyotirupa
Enter name: Krishnanga
Name: Krishnanga
Enter name: none
Name: none