// Using a do-while to process a menu selection
class Menu
{
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 < ' || choice > '5');
System.out.println("\n");
The program compiles OK.
When I run it I get
Help on:
1. if
2. switch
3. while
4. do-while
5. for
Choose one:
Exception in thread "main" java.io.IOException: The handle is invalid
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:199)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at Menu.main(Menu.java:20)
If I run the program from the command line it executes OK. I have used TextPad to code, compile and run about 30 other examples without any problem.
I have used TextPad to code, compile and run about 30 other examples without any problem.
Did any of them read from System.in? My guess is: no.
As far as I know reading from System.in does not work if the java program is started from textpad (as stdin is still connected to Textpad, not to the java program).
It is not an important problem - I am just working through some examples in a text book. If the problem occurs again I will go straight to the command line.
I got too many hangs trying to use System.in and that sort of thing. I tried doing everything from Files and got a major improvement in development time, mostly because of not being stuck staring at a hung box.