itzkreator
itzkreator

Reputation: 1

Problems looping in a while loop with nested if statements

I'm having troubles with the output of my program. I've developed the class 'GetInput' as a constructor which I can reuse when asking various questions for input. Each question asked will need to be equal to or more than a minimum/less then a maximum number which is passed on to the class/constructor. The problem I have is that when the while loop is run, it asked for input four times before finally returning the correct value.

I've added flags which I've worked out when they display. The first shows after input is added the first time. Then the second time, then the fourth time. The fourth time it also displays the flag 'the end' which I want it to reach in one iteration. Why is it looping four times before finally correctly returning the value?

Thanks so much in advance. This is only my second day learning java and this is driving me insane.

import java.util.Scanner; //Import the scanner class

public class main {
public static void main(String[] args) {

    //Set variables to hold the entry cost for each category
    int costAccChild = 2;
    int costUnaccChild = 5;
    int costAdult = 10;
    int costSenior = 8;

    int test = GetInput(0, 1, "Enter a group? (Yes=1/No=0)");
    System.out.println(test);
    System.out.println("the end");

}

 static int GetInput(int min, int max, String request){     
    boolean inputValid = false; //Sets default value to variable for while loop
    int userInput = 0; //Sets default variable for input return

    while (inputValid == false) { //Loops until receives correct input
        System.out.println(request); //Prints question asking for input
        Scanner inputFromUser = new Scanner(System.in); //Ask user for input
        System.out.print("First time"); //FLAG DISPLAYS AFTER FIRST SCANNER

        if (inputFromUser.hasNextInt() == true){ //Check if input has an integer

            System.out.print("Second Time"); //FLAG SHOWS AFTER SECOND SCANNER

            if (inputFromUser.nextInt() >= min && inputFromUser.nextInt() <= max ){ //Check if input is valid
                userInput = inputFromUser.nextInt();
                inputValid= true;

                System.out.print("Fourth time"); //FLAG WORKS FORTH TIME

            }else{ //Input is not correct integer, give error message
                System.out.println("Input is not valid");           
                }   

        }else{ //Input is not an integer at all, give error message
            System.out.println("Input is not valid");
        }
    }
    return userInput; //Returns valid input
    }
}

Upvotes: 0

Views: 1818

Answers (2)

Hardik Mishra
Hardik Mishra

Reputation: 14887

You should store input in some variable and then compare them in the if condition.

This will not block input for further input.

Try this:

public static void main(String[] args) {

    //Set variables to hold the entry cost for each category
    int costAccChild = 2;
    int costUnaccChild = 5;
    int costAdult = 10;
    int costSenior = 8;

    int test = GetInput(0, 1, "Enter a group? (Yes=1/No=0)");
    System.out.println("Return Result: " + test);
    System.out.println("The end");

}

static int GetInput(int min, int max, String request) {
        boolean inputValid = false; //Sets default value to variable for while loop
        int userInputMin = 0, userInputMax=0; //Sets default variable for input return

        while (inputValid == false) { //Loops until receives correct input
            System.out.println(request); //Prints question asking for input
            Scanner inputFromUser = new Scanner(System.in); //Ask user for input
            System.out.print("First time: "); //FLAG DISPLAYS AFTER FIRST SCANNER

            if (inputFromUser.hasNextInt() == true) { //Check if input has an integer
                userInputMin = inputFromUser.nextInt();
                System.out.print("Second Time: "); //FLAG SHOWS AFTER SECOND SCANNER
                if (inputFromUser.hasNextInt() == true) { //Check if input has an integer
                    userInputMax = inputFromUser.nextInt();
                    if (userInputMin >= min && userInputMax <= max) { //Check if input is valid

                        inputValid = true;

                        System.out.println("Third time"); //FLAG WORKS Third Time

                    } else { //Input is not correct integer, give error message
                        System.out.println("Input is not valid");
                    }
                }    
            } else { //Input is not an integer at all, give error message
                System.out.println("Input is not valid");
            }
        }
        return userInputMin; //Returns valid input
    }  

Upvotes: 0

gerrytan
gerrytan

Reputation: 41133

From the manual page http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextLong() :

Both hasNext and next methods may block waiting for further input

It didn't loop 4 times, but whenever you say inputFromUser.hasNextInt() or inputFromUser.nextInt() the Scanner actually blocks waiting for you to input a value.

So this is obviously a bug you have to fix

Upvotes: 2

Related Questions