Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: reprompting for a string?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default reprompting for a string?

    Hi,

    Can someone explain to me how to have a user input information again if their initial one was incorrect? For example, in this it will have the user go back to select an invoice number within range, but how do you do so if they enter words? I've tried next() and nextLine() but it hasn't worked. Thanks for any tips.


     Scanner input = new Scanner(System.in);
      System.out.println("What is the invoice number for your purchase?");
        invoice = input.nextInt();
     
      System.out.println("What is the sale amount of your purchase?");
        amount = input.nextDouble();
     
        while (invoice < 1000 || invoice > 8000)
        {
          System.out.println("You have entered an invalid invoice number.");
          System.out.println("Please select a number between 1000 and 8000.");
          invoice = input.nextInt();//having trouble knowing how to input this if it were a string.
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: reprompting for a string?

    Give us examples of a correct and incorrect responses.

    Why use a string when the input is a number?

    Usually, when a String is used for input, only very few responses are expected. For example, an item from a menu to select which action the program should take next, or a Yes/No answer to choose whether the program should run again. With just a few inputs, a branching statement using an if or switch can be used, and if none of the branches is chosen due to incorrect input, the user is given an error message and asked again for correct input.

    The example you've shown which requires the input to be in a correct numerical range could accept a String input but then the input would be converted to a number (probably an int) to determine if it falls within the correct range:

    int inputValue = Integer.parseInt( inputString );

    Then inputValue can be used to determine if the input was correct.

Similar Threads

  1. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  2. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  3. Replies: 3
    Last Post: October 26th, 2012, 02:19 PM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM