inputting strings

I am sitting in front of three Java books right now and I am having the worst time trying to accomplish the simple task of accepting input in the form of a string and outputting it to the screen via the println statement.

I can get it to accept single character assignments but I have no way to get it to wait until the entire name is entered and enter is pressed.

Can anyone help me out on this most basic of questions?

Thanks a million!

[470 byte] By [kalamazoolou] at [2007-9-26 3:10:44]
# 1

One way is to wrap the console in a BufferedReader.

import java.io.*;

public class ConsoleInputDemo {

public static void main(String[] args) {

System.out.println("Enter your name.");

try {

BufferedReader consoleReader =

new BufferedReader(

new InputStreamReader(System.in));

String name = consoleReader.readLine();

System.out.print("Hello, ");

System.out.println(name);

} catch (IOException e) {

System.err.println("Cannot read from console!");

}

}

}

nerd2004 at 2007-6-29 11:17:33 > top of Java-index,Archived Forums,New To Java Technology Archive...