Is there a way to Buffer in the output from a program which writes to the command line? At the moment, I am using
But that doesn't seem to read any of the output from my program.
My program asks for input at the command line. I have a GUI running simultaneously which uses System.out.print("...") to print coordinates from the clicked sqare to the command line. I want the program to read in the coordinates that appear at the command line as if they were text from the keyboard. I don't know if I've explained that very well? Here's the method After clicking squares in the GUI, the coordinates appear in the right place, but they are not read by the BufferedReader?
1. Your code seems to work fine: to accept keyboard input from a console mode Java application, you must indeed read from System.in, just as you're doing. I cut/pasted your code; here's the output:
2. Perhaps you're confusing reading command line arguments with reading text from stdin. Here's how you can read command line arguments:
3. I'm not sure I've completely understood your question, howevever.
In general, what you're doing *should* work.
One final suggestion: you might want to consider accepting arguments from a Swing GUI. It's really quite easy: certainly not too much more difficult trying to read and parse from System.in!
'Hope that helps .. PSM [ October 23, 2004: Message edited by: Paul Santa Maria ]
Paul M. Santa Maria, SCJP
posted 14 years ago
Hi, thanks for the reply! It doesn't really solve my problem though. If you run the program, and then type in the coordinates (like you did - 1.3 2.5) then it does read them in. However, I am not typing in the co-ordinates. I have a GUI running independently. It is a grid of squares each with their own co-ordinate. When you click on the square, the listener uses System.out.print(...) to print the coordinates to the command line. So the co-ordinates are not typed in using the keyboard, they just appear when you click on a square.
Because of this, the System Reader doens't seem to read them in. The output looks like
So only "hello" is printing out, which means the coordinates are not being read in.
Hope you can help
Paul Santa Maria
posted 14 years ago
Hi, again -
1. I think your original question is answered: a) Yes, "System.in" is the correct way to read with "standard input" from a Java program and
b) Yes, you seem to be doing it correctly.
2. Not certain what exactly you were trying to do, I took a couple guesses that I thought might help: a) Q: Are you clear about the distinction between command-line arguments (which are read from "args") and standard input (which you read from "System.in")? A: The answer appears to be "Yes" - you're definitely trying to read from standard input.
b) Q: would you be inclined to using a GUI instead of stdin/stdout? A: The answer also appears to be "Yes"; you're definitely using a GUI, but I'm not clear if it's Swing ... or Something Else...
3. I've still got many questions (what platform are you on? Are we dealing with one program, or multiple concurrent programs? If so, are both written in Java? Does one invoke the other, or are they both started independently? Etc etc etc). But I really don't think it would be helpful for us to go there.
I've got three suggestions (which may or may not be of help):
a) You might want to modify my (working) test program to try to reproduce the problem. This, in turn, might give you the solution.
b) You might want to consider another way to communicate between your two programs (assuming there *are* two programs). For example: a disk file, a TCP/IP socket or perhaps even JMS.
c) Bear in mind that "communications" involves both:
- The *transport* (standard I/O, file I/O, socket I/O, etc)
- Some form of *synchronization* (is the listener actually ready to receive before the sender transmits?
Again, I hope that helps!
And please: let us know how you ultimately resolve the problem!
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
how do I do my own kindle-like thing - without amazon