Julie Morris
Julie Morris

Reputation: 15

Input having to be typed in twice, and strings are not allowed to be typed in when they can be entered

My Code

package morrisJCh5Sec2;

import java.util.Scanner;

public class Ch5Sec2 {
    public static int collectInteger(String purpose, int minimum, int maximum) {

        Scanner input = new Scanner(System.in);
        System.out.println(purpose);
        System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
        int value = input.nextInt();
        while(input.hasNext()) {

            if(!input.hasNextInt()) {
                System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
                value = input.nextInt();
                input.next();
                continue;
                //not an integer
            }//end not int if
            else {
                value = input.nextInt();
                if(value >= minimum && value <= maximum) {
                    return value;
                }
                else {
                    System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                    System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
                    value = input.nextInt();
                }//end else out of bounds

            }
            //input.close();
        }

        return 0;
    }//end collectInteger
    public static void main(String args[]) {
        final int LOW_INT = 0;
        final int HIGH_INT = 100;
        int inputValue = collectInteger("Enter the number of cats.", LOW_INT, HIGH_INT);
        System.out.printf("Your number of cats was %d.\n", inputValue);
    }//end main
}//end class

My output:

Enter the number of cats.
    Enter an integer between 0 and 100: -56
-56
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: 101
101
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: jads
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at morrisJCh5Sec2.Ch5Sec2.collectInteger(Ch5Sec2.java:30)
    at morrisJCh5Sec2.Ch5Sec2.main(Ch5Sec2.java:42) 

My output should allow for a string to be inputted and it will again ask for an integer. Another issue is that my code is not supposed to make the user type in their input twice, it should just take the user input once and run the code.

Upvotes: 0

Views: 43

Answers (1)

GBlodgett
GBlodgett

Reputation: 12819

You have a lot of extra calls to next() and nextInt() in your code. You can convert your code to a do-while loop. Also you have an extra call to next() in the first if. Then in your inner else you ask for user input for nextInt(), but since you call nextInt() at the beginning of the loop, this is an extra one, which is why it asks for input twice.Also in your first if it handles if the input is not a parsable int. If it is not you have value = input.nextInt(), which will cause an input mismatch exception. Instead call a blank next() to clear the bad input:

public static int collectInteger(String purpose, int minimum, int maximum) {

        Scanner input = new Scanner(System.in);
        System.out.println(purpose);
        System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
        int value;
        do {    
            if(!input.hasNextInt()) {
                System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
                input.next();    //clear bad input                                                     
            }
            else {
                value = input.nextInt();
                if(value >= minimum && value <= maximum) {
                    return value;
                }
                else {
                    System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                    System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");

                }

            }

        } while(input.hasNext());
        return 0;
}

Sample output:

Enter the number of cats.
Enter an integer between 0 and 100: -51
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: 20000
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: Hello
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: 4
Your number of cats was 4.

Upvotes: 1

Related Questions