This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes getting input from the command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting input from the command line" Watch "getting input from the command line" New topic
Author

getting input from the command line

Kalyan Manda
Greenhorn

Joined: Nov 08, 2009
Posts: 2
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

Joined: Jun 10, 2008
Posts: 156

Hi,

Looks like the code you have posted is working.


Priety.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
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

Joined: Oct 13, 2005
Posts: 38087
    
  22
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

Joined: Nov 08, 2009
Posts: 2
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

Joined: Oct 13, 2005
Posts: 38087
    
  22
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting input from the command line
 
Similar Threads
input from console
Passing inputstream to a sub class?
Invoke a Servlet from console ??
Response from modem dissapeared
how can i use shell32 in my java program