1 Reply Latest reply on Oct 7, 2007 12:20 AM by 807605

    String Inputs

      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
          Scanner's nextLine(), JOptionPane.showInputDialog(), BufferedReader's readLine() are all seen here quite often.


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