Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem with System.in.Read()

  Asked By: Mada    Date: May 10    Category: Java    Views: 1842
  

I have the following code:

do
{
System.out.println("Help on:");
System.out.println("\t1. if");
System.out.println("\t2. switch");
System.out.println("\t3. while");
System.out.println("\t4. do-while");
System.out.println("\t5. for\n");
System.out.println("Choose one: ");
choice = (char) System.in.read();
} while (choice < '1' || choice > '5');

When the input is between 1 and 5 included, the program runs fine. But
when the input is out of this range, the menu is redisplayed twice
before waiting for user input. Why is this happening? (The code is from
Java 2, complete reference.)

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Hehet Chalthoum     Answered On: May 10

try to :
1. input  '66' and it will display four times.
2. input '567 , it works OK.
3. input '678', it will display five times.
4. input directly enter, it will display twice.

System.in.read() get the row keyboard input, that means it read  the char  of
'enter' too, and 'enter' is composed of 2 chars '\r\n'. you had better to take
java.io.BufferedReader for a substitution.

 
Answer #2    Answered By: Sean Grant     Answered On: May 10

Thats because you press the enter key and 1st output is for the letter you type
and second for the ENTER key pressed.

 
Answer #3    Answered By: Huette Miller     Answered On: May 10

Well, int is 4 bytes and char  is 2 bytes
long. But even then ur explanation should have been correct. But i tried
to change the code  as
input = System.in.read();
choice = (char) input;
But the problem  remained. However the explanation given by Pallav (See
below) was found to be correct. When i gave the input  9<space>1<enter>it
first redisplayed the menu  twice but the second time automatically went
to the switch  case associated with choice  1.

I guess it stores the input we enter in a buffer and takes its input
from there. So the next question is how do i correct this? That is after
it takes the first input, how do i flush the buffer???

 
Answer #4    Answered By: Maria Miller     Answered On: May 10

Try This,
Though I have not tested it Should Probably work

try{
int choice  ;
do {
System.out.println("Help on:");
System.out.println("\t1. if");
System.out.println("\t2. switch");
System.out.println("\t3. while");
System.out.println("\t4. do-while");
System.out.println("\t5. for\n");
System.out.println("Choose one: ");
//choice = (char) System.in.read();
BufferedReader input  = new BufferedReader(new
InputStreamReader(System.in));
String str = input.readLine();

try{
choice = Integer.parseInt(str);
}catch(NumberFormatException e){
choice = 0 ;
}

} while (choice < 1 || choice > 5);

}catch(Exception e){
e.printStackTrace();
}

 
Didn't find what you were looking for? Find more on Problem with System.in.Read() Or get search suggestion and latest updates.




Tagged: