• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

read data from keyboard

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your kind reference.
Andrew
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic