I have done this task but i don't understand why was this Code is not correct and the result was ( 98 ) 🤯
int num = reader.read(); Why wthis this Code dosn't worked the result was ( 98 )
int num = Integer.parseInt(reader.readLine()); Why to int ? it's worked but i dont understand why
Why?
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Misiu
13 October 2020, 13:35
What, where are you doing?
Method read() reads one byte from stream and returns it as int type.
0
Ahmad Dabagh
14 October 2020, 06:26
This is my Code with the mistake
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int num = reader.read();
if ( num > 0 )
{
num = num * 2;
System.out.println( num );
}
else if ( num < 0 )
{
num = num + 1;
System.out.println(num);
}
else
System.out.println(0);
}
}
0
Misiu
14 October 2020, 12:20solution
read() reads byte and returns int.
If 5 is entered, the metod returns 53 as int type.
53 is ASCII code for 5.
readLine() reads line and returns it as a string.
Integer.parseInt(string) returns int from given string.
If 5 is entered, readLine() returns "5" and then Integer.parseInt("5") returns 5.
+2
Ahmad Dabagh
14 October 2020, 12:41
Thank you very much 🌹
0