Forums Register Login

Read keyboard IO methods hangs.

+Pie Number of slices to send: Send
Hi all,
I am missing something in the IO reading stakes. The following method runs but doesn't exit after I have entered a keyboard number.
What am I fundamentally doing wrong?
Any help appreciated.
Stan.

public int createLoc() throws IOException
{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader stdin = new BufferedReader(isr);
try
{
System.out.print("Enter type : 1=home, 2=office, 3=car: ");
type = Integer.parseInt(stdin.readLine());

}


catch(IOException ioe)
{
System.out.println("no good");
}
stdin.close();

System.out.println(type+" was entered");
return type;


}
+Pie Number of slices to send: Send
Stan,
Are you pressing enter after entering the number? readLine() requires the enter key to get a full line.

Where does your method hang?
+Pie Number of slices to send: Send
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
+Pie Number of slices to send: Send
I tried out your code, and it worked fine, after pressing enter as Jeanne suggested.
+Pie Number of slices to send: Send
Thanks for advise, where my problem is is that I am creating a GUI window and with this code inserted the window won't start up? This code is to test prior to incorporating in GUI.
I won't bother searching as to why this happens, but from you guys looking at what I posted it appears my structure is OK? for reading the keyboard.

Thanks again to all.
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 850 times.
Similar Threads
Run .sh file using java
Help wanted to put correct Loop type....
urgent help! my while loop is not working
Reading from keyboard
Simple problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:02:50.