package com.learn.java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferReaderInput {
public static void main(String[] args) throws IOException {
BufferedReader bufferreaderIn = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter your Name");
String Name = bufferreaderIn.readLine();
System.out.println("Enter your age");
int age = Integer.parseInt(bufferreaderIn.readLine());
System.out.println("Enter your salary");
int sal = bufferreaderIn.read();
System.out.println("Hi, I'm " + Name + " my age is " + age
+ " and my salary is " + sal);
}
}
When I enter salary using obj.read(); it is not giving the right output.
With that can anyone tell me what is the difference between read() and readline()?
As from the documentation of BufferedReader, we have this for read:
Reads a single character.
And this for readLine:
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
So, set apart the details of when a line is considered to be terminated, the difference is that the first one read a single character whilst the second one read a whole line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With