• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read keyboard IO methods hangs.

 
Stan Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;


}
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35716
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,
Are you pressing enter after entering the number? readLine() requires the enter key to get a full line.

Where does your method hang?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried out your code, and it worked fine, after pressing enter as Jeanne suggested.
 
Stan Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!