Input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    #1

    Input

    How does the input function in java work?
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    Originally posted by Kid Programmer
    How does the input function in java work?
    What input method, in what object, in what library?

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by pronerd
      What input method, in what object, in what library?
      Input for strings and numbers. I don't know how to answer your questions. I'm new to java. Just do any of them.

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        Originally posted by Kid Programmer
        Input for strings and numbers. I don't know how to answer your questions. I'm new to java. Just do any of them.
        Neither String or Number class has an input method. What is it you are trying to do?

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Are you possibly asking how to input in Java?

          Comment

          • Kid Programmer
            New Member
            • Mar 2008
            • 176

            #6
            All I want to know is how you can prompt the user to enter a number or string.

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              Prompting (aka output) can be done through System.out.prin tln(), or System.out.prin t() if you don't want the newline. As to input, Scanner is what you want. You also might want to look at Sun's Java tutorials for all sorts of useful information.

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                If you're using 1.6, I like java.io.Console in simple situations: http://java.sun.com/javase/6/docs/ap...o/Console.html

                Comment

                • Kid Programmer
                  New Member
                  • Mar 2008
                  • 176

                  #9
                  System.out.prin tln and System.out.prin t simply make a message appear. I want the user to actually be able to enter something. In the language python for example the command would be:

                  blah = input("please enter a number or word: ")

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    Hence the link to the Scanner class I posted. The example code there shows how to use it as console-based input. Just remember to put 'import java.util.Scann er;' at the top of your file to use it. You have to use two commands to do the same thing as input("blah") in Python. One to output the string, one to read the input from the user.

                    Comment

                    • Kid Programmer
                      New Member
                      • Mar 2008
                      • 176

                      #11
                      Okay I got it to work. Thanks. One more question. Is it possible to have the user only input a number.

                      Comment

                      • sukatoa
                        Contributor
                        • Nov 2007
                        • 539

                        #12
                        Originally posted by Kid Programmer
                        Okay I got it to work. Thanks. One more question. Is it possible to have the user only input a number.
                        In console? That is impossible... but you can prompt the user if it is a wrong input.....

                        regards,
                        sukatoa

                        Comment

                        • Kid Programmer
                          New Member
                          • Mar 2008
                          • 176

                          #13
                          Originally posted by sukatoa
                          In console? That is impossible... but you can prompt the user if it is a wrong input.....

                          regards,
                          sukatoa
                          How can you prompt the user if it is a wrong input?

                          Comment

                          • sukatoa
                            Contributor
                            • Nov 2007
                            • 539

                            #14
                            Originally posted by Kid Programmer
                            How can you prompt the user if it is a wrong input?
                            Capture the user's input... ( variable should handle this )...

                            Compare it with what you want.... since it should be a number....

                            Convert it first into arrays of characters.
                            Loop in every element and compare it with digits... ( depends on how you will implement them )....

                            or simply put a try/catch block inside the conversion code...

                            Code:
                            try{
                                 int digit = new java.util.Scanner(System.in).nextInt();
                            }catch(InputMismatchException em){
                                 new javax.swing.JOptionPane().showMessageDialog(null,
                                           "BAD INPUT!!");
                            }
                            regards,
                            sukatoa

                            Comment

                            • Laharl
                              Recognized Expert Contributor
                              • Sep 2007
                              • 849

                              #15
                              You can also use the Scanner's hasNextInt() method with a while loop to check if the user has entered an integer before you try to grab it or parse it at all.

                              Comment

                              Working...