Reading the Input from the console is very easy and developers may use it without even thinking. Same like that is very important to understand what are the different ways of reading it, when to use the approach.
There are three ways we can read the input from the user using the console.
1.Using Buffered Reader Class.
2.Using Scanner Class.
3.Using Console Class.
Difference Between println and printf.
println() prints a new blank line and then your message. printf() provides string formatting similar to the printf function in C. printf() is primarily needed when you need to print big strings to avoid string concatenaion in println() which can be confusing at times. (Although both can be used in almost all cases).
Eg. int a = 10;int b = 20;
println("a: " + a + " b: " + b); //Tedious String Concatenation
printf("a: %d b: %d\n", a, b); // Output using string formatting.
While printf() enables you to print fractional outputs up to any decimal place in a single line, the same task using println() can get very complex.
Eg. printf("%.6f",x); // prints x up to 6 decimal places.
There are three ways we can read the input from the user using the console.
1.Using Buffered Reader Class.
2.Using Scanner Class.
3.Using Console Class.
Using Buffered Reader Class
|
Using Scanner Class
|
Using Console Class
|
This is used to read the result include in the order line.
|
This is used to parse primitive typesand strings using regular expressions.
|
Preferred way of reading the innour from the Console.
|
Advantage: The Input is Buffered for efficinnt reading
|
Advantage: Convinient for Primitive types and using the regular expressions.
|
Advantage: Reading password without echoing the entered chars.
Reading methods are Synchronized
|
Drawbacks: The Wrapping code is hard to remember.
|
Drawbacks: The Reading methods are not synchronized.
|
Drawbacks: Does not work in non-interactive environement.
|
Usage:
String buffer=null;
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter the Buffer terms:");
buffer=buf.readLine();
System.out.println(buffer);
} catch (IOException e) {
e.printStackTrace();
}
|
Usage:
String scanner=null;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Scanner terms:");
scanner=sc.nextLine();
System.out.println(scanner);
|
Usage:
String console=null;
Console con = System.console();
System.out.println("Enter the Console terms");
console=con.readLine();
con.printf("%s\n", console);
|
Output:
Enter the Buffer terms:
Syed Ghouse Habib
Syed Ghouse Habib
|
Output:
Enter the Scanner terms:
Syed Ghouse Habib
Syed Ghouse Habib
|
Output:(Note: I am using IDE)
Exception in thread "main" Enter the Console terms
java.lang.NullPointerException
at com.searchendeca.thread.sample.ReadingInput.main(ReadingInput.java:30)
|
Difference Between println and printf.
println() prints a new blank line and then your message. printf() provides string formatting similar to the printf function in C. printf() is primarily needed when you need to print big strings to avoid string concatenaion in println() which can be confusing at times. (Although both can be used in almost all cases).
Eg. int a = 10;int b = 20;
println("a: " + a + " b: " + b); //Tedious String Concatenation
printf("a: %d b: %d\n", a, b); // Output using string formatting.
While printf() enables you to print fractional outputs up to any decimal place in a single line, the same task using println() can get very complex.
Eg. printf("%.6f",x); // prints x up to 6 decimal places.
No comments:
Write comments