0
    public int displayMenuGetChoice(){


    System.out.println("Specialty Auto");
    System.out.print("\n(1) Display the Entire automobile list"
                       + "\n(2) Display the entire automobile inventory sorted by year"
                       + "\n(3) Display automobiles available for sale"
                       + "\n(4) Display list of recently sold automobiles"
                       + "\n(5) Output a mailing list of recent buyers"
                       + "\n(6) Find automobile by VIN number"
                       + "\n(7) Quit"
                       + "\n\nEnter your choice (1-7): ");
        while (!keyboard.hasNextInt() || keyboard.nextInt() < 1 || keyboard.nextInt() > 7){
            System.out.println("Invalid Input");
            System.out.println("Specialty Auto");
            System.out.print("\n(1) Display the Entire automobile list"
                               + "\n(2) Display the entire automobile inventory sorted by year"
                               + "\n(3) Display automobiles available for sale"
                               + "\n(4) Display list of recently sold automobiles"
                               + "\n(5) Output a mailing list of recent buyers"
                               + "\n(6) Find automobile by VIN number"
                               + "\n(7) Quit"
                               + "\n\nEnter your choice (1-7): ");
        keyboard.nextInt();
        }       

        return keyboard.nextInt();

}

Here is what I have, I need to validate to make sure the user is inputting a number 1 through 7 but also not entering any letters, it works sort of. When I test it for when I dont make an intentional error (using the number 1) I have to enter it 3 times before the proper dialog comes up. what am I doing wrong?

edit: I'm not sure why you tagged to that other post, it didn't clear anything up for me it seems like a similar but different question.

  • Why are you calling `nextInt()` so many times? (This is a rhetorical question.) – Joe C Nov 11 '17 at 20:46
  • So i should do what, store it into a string variable and then parse it into an integer? wont I get an error if the user enters letters? – Michael Albert Nov 11 '17 at 20:56

0 Answers0