• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting input from the command line

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have written this this small function which gets input from the command line. Basically this the same part of the code which was working fine in the other part of the code.

void viewCourseDetails(String login)
{
try
{
String CourseId;
System.out.println("Enter the Course Id: \n");

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(isr);

CourseId = input.readLine();
}
catch ( IOException e )
{
System.out.println("IOError: "+ e.getMessage());
}
}


Here i have to get the value of CourseId from command line. The code compiles fine but at run time it simply prints the System.out statement and exits without any error.
Can anybody please tell me why this is happening?
 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Looks like the code you have posted is working.

 
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey kalyan,

The part of the code which you have posted is working fine. Just add a System.out.println(CourseId); after the line and it will print. May be some other exception in other part of the code.

If nothing confidential you can post your full code :-). We will help you to trace the error..
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please use the CODE button and maintain indentation; then you code will be much easier to read.

I presume you are closing your Readers in a finally block in your "real" code? Why aren't you using a Scanner instead? It is usually easier to use for text input.
 
Kalyan Manda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, thanks for your response. It helped me a lot.

I couldn't post the entire code (around 300 lines) as it was very big and also had jdbc related stuff. So probably it would have not compiled on your machines.
Also I had used a lot of Buffered readers and probably that's the reason why some part of the code was not working fine.

But now I used the Scanner class based on Campbell's suggestion and the code is working just fine.

Also please forgive me for the lack of proper indentation in my code, am a novice here.

Thanks for all your help guys.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic