• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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
Sheriff
Posts: 38569
659
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!