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? ;) I can't get it to work and I'm a Java newbie :(

Re: How do I read a line? 80 80

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(;

// get filename from console:
String filename = in.readLine();
Re: How do I read a line? 80 80

hi babe.
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.

Re: How do I read a line? 80 80

String str = JOptionpane.showInputDialog(null,"Enter String");

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.