dan._.theman
dan._.theman

Reputation: 57

Why do I have to enter two inputs for my code to start running?

I'm using a while statement in my code, where, inside the while statement, the user inputs a number. To stop the program from looping, the user must input the word "stop". However, once I enter in a number, the output skips to another line without printing the statement I want it to print, and I have to enter my desired input again for the program to start looping. The only time this problem DOES NOT occur is when the user inputs "stop" FIRST, then the code works fine.

This is to find the max, min, and mean of any amount of user-inputted numbers. I've tried changing the order of the else/if statements and the parameters for the said else/if statements, but nothing seems to work.

    boolean stopped = false;

    int numberAmount = 0;
    int invalidAmount = 0;

    double max = Integer.MIN_VALUE;
    double min = Integer.MAX_VALUE;
    double mean = 0;        

    while(stopped == false)
    {   
        System.out.print("Enter a number (type "+"\""+"stop"+"\""+" to stop): ");
        String originalInput = userInput.nextLine();

        if(originalInput.equals("stop"))
        {
            stopped = true;
            invalidAmount ++;
            System.out.println(numberAmount+" numbers were entered with "+invalidAmount+" invalid inputs.");
        }

        else if(userInput.hasNextDouble())
        {
            double currentValue = Double.parseDouble(originalInput);
            max = Math.max(max, currentValue);
            min = Math.min(min, currentValue);
            mean = currentValue;
            numberAmount ++;
        }

        else if(originalInput.equals("stop") == false)
        {
            System.out.println("Not a number...");
            invalidAmount ++;
        }
    }


    System.out.println("The maximum is "+max+".");
    System.out.println("The minimum is "+min+".");
    System.out.println("The mean is "+(mean / numberAmount)+".");

    userInput.close();

}

}

For example, I expect the output after inputting 7 to be "Enter a number (type "stop" to stop):" on the next line(since the program loops to keep prompting for number input), where the user could then keep inputting numbers as they like. Instead, the actual output is a blank line under the original prompt for user input, where the user must input their desired input AGAIN for the program to start looping.

Upvotes: 0

Views: 845

Answers (3)

Nel
Nel

Reputation: 154

I think you should invert the logic of the code, assuming you are using the Scanner, try something like this:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        boolean stopped = false;

        int numberAmount = 0;
        int invalidAmount = 0;

        double max = Integer.MIN_VALUE;
        double min = Integer.MAX_VALUE;
        double mean = 0;

        while (stopped == false) {

            Scanner userInput = new Scanner(System.in);
            System.out.print("Enter a number (type " + "\"" + "stop" + "\"" + " to stop): ");

            if (userInput.hasNextDouble()) {
                double currentValue = userInput.nextDouble();
                max = Math.max(max, currentValue);
                min = Math.min(min, currentValue);
                mean = currentValue;
                numberAmount++;

            } else {
                String originalInput = userInput.nextLine();
                if (originalInput.equals("stop")) {
                    stopped = true;
                    invalidAmount++;
                    System.out.println(numberAmount + " numbers were entered with " + invalidAmount + " invalid inputs.");
                } else {
                    System.out.println("Not a number...");
                    invalidAmount++;
                }
            }
        }


        System.out.println("The maximum is " + max + ".");
        System.out.println("The minimum is " + min + ".");
        System.out.println("The mean is " + (mean / numberAmount) + ".");

        // userInput.close();
    }
}

Basically you are checking first the input type, and only after you are collecting the value from the console. Doing it the way you have right now, you will always ask for the second input.

Upvotes: 1

Kaan
Kaan

Reputation: 5754

You didn't specify in your code example what userInput is, but from the usage it looks to be an instance of Scanner. If you have a Scanner declared and then call hasNextDouble(), you will get a boolean result which fits with your usage – you have that as the condition in your if statement.

Scanner userInput = new Scanner(System.in);
boolean b = userInput.hasNextDouble();

What's missing from the picture is how hasNextDouble() works. Looking at the Javadoc for Scanner:

Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. The scanner does not advance past any input.

In order to answer true/false for whether the next input is a double or not, the scanner has to wait for input from the user before it can proceed.

All of this to say: your code looks like it's behaving normally. If you don't want to wait for user input, you need to write your code to reflect that.

Upvotes: 1

Ion Freeman
Ion Freeman

Reputation: 540

I don't really know the API, but I expect hasNextDouble reads another line. Check if originalInput is a double, don't read another line.

Upvotes: 0

Related Questions