1 Reply Latest reply: Oct 6, 2007 7:20 PM by 807605 RSS

    String Inputs

    807605
      Hey, I'm still pretty new to Java and got stuck on something. How can I have the String that I have created be inputed by the user. What I want is to have:

      String x = "user input here"

      Thanks in advance for any help.
        • 1. Re: String Inputs
          807605
          Scanner's nextLine(), JOptionPane.showInputDialog(), BufferedReader's readLine() are all seen here quite often.

          http://java.sun.com/javase/6/docs/api/java/util/Scanner.html
          http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html
          http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html

          Have a read "Basic I/O" in Sun's Tutorial: http://java.sun.com/docs/books/tutorial/essential/io/index.html

          The "The Java Developers Almanac 1.4" is a great source of examples. They have one for BufferedReader: http://www.exampledepot.com/egs/java.io/ReadFromStdIn.html

          There is a techtip dealing with the Scanner class here: http://java.sun.com/developer/JDCTechTips/2004/tt1201.html But it is quite involved - the API documentation may be just as good for what you want.

          [Edit] I forgot java.io.Console because I don't use it.
          import java.io.Console;
          
          public class Test {
              public static void main(String args[]) {
                  Console con = System.console();
                  String x = con.readLine("Enter input --> ");
                  System.out.println("You entered: " + x);
              }
          }