jaro21
jaro21

Reputation: 21

why java BufferedReader miss output

I got error from my code,

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.print("a    = ");
int a=in.read();
System.out.print("b    = ");
int b=in.read();

System.out.print("    = "+a);
System.out.print("b    = "+b);

i try to input 1, and i dont understand why the result like this?

a    = 1

b    =     = 49b    = 13

Where is the second input going?

Upvotes: 1

Views: 40

Answers (1)

Pritam Banerjee
Pritam Banerjee

Reputation: 18968

You can try something like this:

a = in.readLine();
System.out.print("b    = ");
String b=in.readLine();
int aInt = Integer.valueOf(a);
int bInt = Integer.valueOf(a);
System.out.print("a    = "+aInt);
System.out.print("b    = "+bInt);

read() reads character by character, so newline will be counted as new character. To read more about it you can read here.

Upvotes: 2

Related Questions