• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read keyboard IO methods hangs.  RSS feed

 
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;


}
 
author & internet detective
Marshal
Posts: 38391
650
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?
 
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.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!