Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read data from keyboard

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your kind reference.
Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic