There
are many ways to read data from the keyboard. For example:
- InputStreamReader
- Console
- Scanner
- DataInputStream etc.
InputStreamReader class: InputStreamReader
class can be used to read data from keyboard.It performs two tasks:
- connects to input stream of keyboard
- converts the byte-oriented stream into character-oriented stream
BufferedReader class:
BufferedReader
class can be used to read data line by line by readLine() method.
Example of reading data from keyboard by InputStreamReader
and BufferdReader class:
In
this example, we are connecting the BufferedReader stream with the
InputStreamReader stream for reading the line by line data from the keyboard.
import java.io.*;
class G5{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println("Enter your name");
String name=br.readLine();
System.out.println("Welcome "+name);
}
}
Output:Enter
your name
Amit
Welcome Amit
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.*;
class G5{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name="";
while(name.equals("stop")){
System.out.println("Enter data: ");
name=br.readLine();
System.out.println("data is: "+name);
}
br.close();
r.close();
}
}
Output:Enter
data: Amit
data is: Amit
Enter data: 10
data is: 10
Enter data: stop
data is: stop
Java Console class: The
Java Console class is be used to get input from console. It provides methods to
read text and password. If
you read password using Console class, it will not be displayed to the user. The
java.io.Console class is attached with system console internally. The Console
class is introduced since 1.5.
Let's
see a simple example to read text from console.
String text=System.console().readLine();
System.out.println("Text is: "+text);
Methods of Console class
Let's
see the commonly used methods of Console class.
Method
|
Description
|
1) public String readLine()
|
is used to read a single line of
text from the console.
|
2) public String readLine(String
fmt,Object... args)
|
it provides a formatted prompt
then reads the single line of text from the console.
|
3) public char[] readPassword()
|
is used to read password that is
not being displayed on the console.
|
4) public char[]
readPassword(String fmt,Object... args)
|
it provides a formatted prompt
then reads the password that is not being displayed on the console.
|
How to get the object of Console
System
class provides a static method console() that returns the unique instance of
Console class.
- public static Console console(){}
Let's
see the code to get the instance of Console class.
- Console c=System.console();
Java Console Example
import java.io.*;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("Welcome "+n);
}
}
Output:
Enter
your name: james gosling
Welcome
james gosling
Java Console Example to read password
import java.io.*;
class ReadPasswordTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter password: ");
char[] ch=c.readPassword();
String pass=String.valueOf(ch);//converting char array into string
System.out.println("Password is: "+pass);
}
}
Output:
Enter
password:
Password
is: sonoo
Java Scanner class: There
are various ways to read input from the keyboard; the java.util.Scanner class
is one of them. The Java Scanner class breaks the input into tokens
using a delimiter that is whitespace bydefault. It provides many methods to
read and parse various primitive values. Java Scanner class is widely used to
parse text for string and primitive types using regular expression. Java
Scanner class extends Object class and implements Iterator and Closeable
interfaces.
Commonly used methods of Scanner class: There
is a list of commonly used Scanner class methods.
Method
|
Description
|
public String next()
|
it returns the next token from the
scanner.
|
public String nextLine()
|
it moves the scanner position to
the next line and returns the value as a string.
|
public byte nextByte()
|
it scans the next token as a byte.
|
public short nextShort()
|
it scans the next token as a short
value.
|
public int nextInt()
|
it scans the next token as an int
value.
|
public long nextLong()
|
it scans the next token as a long
value.
|
public float nextFloat()
|
it scans the next token as a float
value.
|
public double nextDouble()
|
it scans the next token as a
double value.
|
Java Scanner Example to get input from console
Let's
see the simple example of the Java Scanner class which reads the int, string
and double value as an input:
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}
Output:
Enter your rollno
111
Enter your name
Ratan
Enter
450000
Rollno:111 name:Ratan fee:450000
Java Scanner Example with delimiter
Let's
see the example of Scanner class with delimiter. The \s represents whitespace.
import java.util.*;
public class ScannerTest2{
public static void main(String args[]){
String input = "10 tea 20 coffee 30 tea buiscuits";
Scanner s = new Scanner(input).useDelimiter("\\s");
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.nextInt());
System.out.println(s.next());
s.close();
}
}
Output:
10
tea
20
coffee
No comments:
Post a Comment