Quote:
Originally posted by Jose Muņiz
Code:
import java.io.*;
public class ExtraTime
{
public static void main(String args[]) throws IOException
{
int hours = 0;
System.out.print("Insert hours you worked <-1 to finish>: ");
System.out.flush();
hours = System.in.read();
System.in.skip(1);
System.out.println("Hours = " + hours);
}
}
|
Let us take this part of this code....
What happens with system.in.read is
Say you take input as "a" then,it takes input as 97 which is the int value of a
Similarly when you give input as "12" . It s taking only 1 as input and
outputting 49 which happens to be the int value of char 1
If you know C, I can show you a similar thing...
Suppose int ch;
When you try to get it using getchar() , you don't get the entire
number say "123" only one character will be accepted . Here it s 1 and so, 49 will be outputted. Suppose "23456" then the value of 2 i.e,50 will be outputted
Summary :-
What I would like to say is that same as getchar() cannot be used to
accept integers in C ,
In Java system.in.read should not be used to accept integers.
It can be used to get characters one - by - one
USE READLINE INSTEAD....