Forums Register Login

getting input from the command line

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Hi,

Looks like the code you have posted is working.

+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
You're welcome
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 966 times.
Similar Threads
Passing inputstream to a sub class?
how can i use shell32 in my java program
Response from modem dissapeared
input from console
Invoke a Servlet from console ??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:25:47.