GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes read data from keyboard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read data from keyboard" Watch "read data from keyboard" New topic
Author

read data from keyboard

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
I want to let user input a string, display it and then terminate the program.
I have the following code:
import java.io.*;
public class MyCat {
public static void main (String args[])
throws IOException {
int b;
while ((b = System.in.read()) != -1) {
System.out.print((char)b);
}
System.out.println();
}
}
The result is that it requires input continueously without termination.
I changed it:
import java.io.*;
public class MyCow {
public static void main (String args[])
String b;

System.in.read(b);
System.out.print(b);
}

It shows a lot of errors. I don't understand why do I need to use Exception in the first case, the while loop is to read the char one by one.
In C++, I just cin >> a; and cout << a; to get and display the string a. How should I do it in Java?

Thanks
Andrew
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Andrew,
The code below might be what you are looking for.

You should usually use BufferedReader to handle input from the console.
Regards,
Manfred
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Thanks a lot and I will re-read the BufferReader method again.
Have you used any IDE such as JBuilder or VisualAge? Which one can create the AWT-GUI like VB6?
Regards
Andrew
Reuben Cleetus
Ranch Hand

Joined: Jul 13, 2001
Posts: 50
Andrew,
You can use Netbeans (www.netbeans.org), Forte (www.sun.com/forte), JBuilder (www.jbuilder.com), or any other Java IDE to develop GUI forms.
Of course, if you can also get the same results using a plain vanilla text editor, but you'll have to brush up on Swing/AWT APIs for that. It's good practice though, and fear not, the Java APIs for GUIs are MUCH easier than Windows APIs!
Regards,
Reuben.
Originally posted by Andrew Parker:
Hi,
Thanks a lot and I will re-read the BufferReader method again.
Have you used any IDE such as JBuilder or VisualAge? Which one can create the AWT-GUI like VB6?
Regards
Andrew

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks for your kind reference.
Andrew
 
GeeCON Prague 2014
 
subject: read data from keyboard