How can I read a string in from the user? For example, I am writing a console-based java application which prompt the user to enter a string and then reads that string in. What is the opposite of System.out.println? ;) System.in.readln? I can't get it to work and I'm a Java newbie :(
A friend of mine helped me out. I was able to solve this dilemma via the following code:
// prompt user for filename:
System.out.print("Enter filename: ");
// in is a file stream to read from the console:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// get filename from console:
String filename = in.readLine();
if you wana read just a string the simple approach is by using "JOptionPane" dialogs import javax.swing.*;
and where ever you need to get string from user try this syntex
String str = JOptionpane.showInputDialog(null,"Enter String");
now you can use str where ever in your program.
Ah, but then you miss all of the stream stuff, which is quite handy for any sort of input/output situation (and it makes you tough, too). Never mind that (warning: opinion ahead) throwing up a dialog box just to get an input string is annoying and should be punished.