Reputation: 43
I am learning java, and I came across one sample program in the book I am learning from. I decided to experiment a little and I don't understand why it doesn't work as expected. Program is very simple, it's a menu that should output general forms of some of Java's statements based on users input. Here's the code:
public class SimpleMenu {
public static void main(String args[])
throws java.io.IOException {
int choice;
do {
System.out.println("Help on: ");
System.out.println(" 1. IF");
System.out.println(" 2. WHILE");
System.out.println(" 3. DO WHILE");
System.out.println(" 4. FOR");
System.out.println(" 5. SWITCH\n");
System.out.println("Choose one: ");
choice = System.in.read();
} while ((choice < 1) || (choice > 5));
System.out.println("\n");
switch (choice) {
case 1:
System.out.println("if(condition) statement1;\n else statement2;");
break;
case 2:
System.out.println("while(condition){\n //body of loop \n }");
break;
case 3:
System.out.println("do{ \n //body of loop \n } while(condition);");
break;
case 4:
System.out.println("for(initialization; termination; increment/decrement){ \n statement; \n }");
break;
case 5:
System.out.println("switch(value){\n case 1: \n statement; \n break; \n default:\n statement;\n}");
break;
}
}
}
No matter what number I type in, it goes through do-while loop two times and expects input again. This is the original code:
public class MenuFromBook {
public static void main(String args[])
throws java.io.IOException {
char choice;
do {
System.out.println("Help on: ");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\n");
System.out.println("Choose one:");
choice = (char) System.in.read();
} while( choice < '1' || choice > '5');
System.out.println("\n");
switch(choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" //...");
System.out.println("}");
break;
case '3':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '4':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
case '5':
System.out.println("The for:\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
}
But I don't understand why it has to be done like that, why can't choice be int and work like that? I hope I was clear...
Upvotes: 0
Views: 80
Reputation: 1114
System.in
is an InputStream
. The read()
-method reads one byte of data. In this case, you input ASCII 1 (byte = 49). Try printing choice to see its value, which should be 49 in case you enter 1.
https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29 http://www.asciitable.com/
Upvotes: 1
Reputation: 53462
System.in.read() reads input from your keyboard as ascii characters, and if you look at ascii chart, you'll see that numbers 0-9 are actually in the range of 48-57. Thus, if you would compare the result to the actual ascii codes, your code would work correctly. Using '1'
you would do just that: '1'
is int 49, so comparison works.
Upvotes: 0