Page 1 of 1

IOException query

Posted: Sat May 17, 2008 4:40 am
by Alan-LB
I am coding an example from a text book

The start of the program is

Code: Select all

//  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.

Thanks

Alan

Posted: Sat May 17, 2008 5:40 am
by MudGuard
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).

Posted: Sat May 17, 2008 6:26 am
by Alan-LB
You are right - none of the other programs read from System.in. This explains my problem - how can I get around it?

Thanks for your help

Alan

Posted: Sat May 17, 2008 12:12 pm
by MudGuard
Don't start your program from textpad, start it from command line

Posted: Sun May 18, 2008 4:39 am
by Bob Hansen
Maybe that command line could be in a barch file that is started from a TextPad tool?

Posted: Fri May 23, 2008 7:08 am
by Alan-LB
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.

Thanks for your help

Alan

use files

Posted: Sat May 31, 2008 8:02 pm
by Nicholas Jordan
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.