• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting input from the command line

 
Kalyan Manda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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?
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Looks like the code you have posted is working.

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • 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..
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic