Reputation: 21
I am just a beginner in java coding i was just writing a simple program : user is given a menu he has to enter number between 1-4 if user enters correct number, required task is done if wrong number is entered,user is again asked for input. Below is my program
class menu {
public static void main(String [] args) throws java.io.IOException {
int choice;
do
{
System.out.println("HELP MENU: ");
System.out.println("IF STATEMENT: 1 ");
System.out.println("WHILE: 2 ");
System.out.println("DO WHILE: 3 ");
System.out.println("SWITCH: 4 ");
choice = System.in.read();
System.out.println(choice);
}
while( choice < 1 || choice > 4);
System.out.println("\n");
System.out.println(choice);
switch (choice)
{
case 1:
System.out.println("if statement is selected");
break;
case 2:
System.out.println("while statement is selected");
break;
case 3:
System.out.println("do while statement is selected");
break;
case 4:
System.out.println("switch statement is selected");
break;
}
}
}
OUTPUT: +++++++
E:\study\javacode>java menu
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
4
52
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
13
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
10
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
what ever user is entering through keyboard ,the code keeps on iterating through do-while loop.i identified the cause by printing the input value and what i found is that input value is taken wrong by the code.Please help to solve this
Upvotes: 2
Views: 494
Reputation: 18504
System.in.read()
does not quite work like you think it does. It reads a character (not an int) and returns a byte value, not a integer.
When a user enters "1", read()
returns 49, which is the integer byte value for character '1'. (50 is '2', 51 is '3', etc).
@Christian's scanner suggestion is very good. I think you should do that instead.
Alternately, you can change your switch
statement to use 49/50/51/etc, but that's kind of ugly.
Upvotes: 0
Reputation: 34176
To read numbers or String
s, I would recommend you to use a Scanner
object. Create and instantiate it at the begining of the main()
:
Scanner in = new Scanner(System.in);
and call the nextInt()
method in the do-while
:
do {
System.out.println("HELP MENU: ");
System.out.println("IF STATEMENT: 1 ");
System.out.println("WHILE: 2 ");
System.out.println("DO WHILE: 3 ");
System.out.println("SWITCH: 4 ");
choice = in.nextInt();
System.out.println(choice);
} while (choice < 1 || choice > 4);
Notes:
System.in.read()
is actually returning the int
value of the character you input. For example if you input 1
, the method will return 49
which is equal to (int)'1'
Scanner
to read String
s, with the method nextLine()
.Edit:
Just to see that you can use System.in.read()
(you must read the documentation to understand what it does) to read an integer, try this (in a separate file so you don't accidentally modify your code):
int i = System.in.read();
System.out.println(Integer.parseInt(Character.toString(((char) i))));
Upvotes: 2
Reputation: 3279
The issue is that you're using InputStream.read(), which reads a byte from the stream, not a character, e.g. if you type '1', read() will return 0x31.
Javadoc of read():
/**
* Reads the next byte of data from the input stream. The value byte is
* returned as an <code>int</code> in the range <code>0</code> to
* <code>255</code>. If no byte is available because the end of the stream
* has been reached, the value <code>-1</code> is returned. This method
* blocks until input data is available, the end of the stream is detected,
* or an exception is thrown.
*
Upvotes: 1