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

how to get continuous input from user in java
java program to add two numbers taking input from user
how to take integer input from user in java
how to take multiple string input in java using scanner
java user input string
python while loop user input
how to take multiple inputs in java
how to ask for user input in c#

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.


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.

Beginning Programming with Java For Dummies, To go from Listing 6-1 to Listing 6-2, I added an import declaration and some stuff You can find out more about these things by reading the section on input and For example, when you start running the code in Listing 5-1, you have this  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.


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.

The Internet Encyclopedia, A CGI program can also be run when a page is loaded (often used for Web counters) by for HTML) with the string Content-type: text/html, followed by two carriage returns. The minimum required would be the start and end BODY and HTML codes. A GET method will provide the user's input to the CGI program as an  The "While" Loop . A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".


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.

Implementing User Input in C# Programming, You display a prompt asking the user to enter two numbers. The numbers are then The second statement you need is the input statement. If you compile the above code, it will run through the program until the end of the Main function is found. C programs start with a function (or method in this case) named Main. The  I enter my passcode again and get the same message. If I enter it a 3rd time I get a warning and have to start from the beginning again. There is an option to enter a passcode from a different device, so I choose my iPad and enter the code. When I go back to settings the follow up flag is still there and I am right back to the very start. I


java for complete beginners - user input, How to get input from a user using the command line in java. One of the strengths of Java is the huge libraries of code available to you. The difference between the two is that println will move the cursor to a new line after the output, but print stays Run your programme until your Output window displays the following:. If you use iOS 11.3 or later on your iPhone, you might not need to enter a verification code. In some cases, your trusted phone number can be automatically verified in the background on your iPhone. It’s one less thing to do, and your account is still protected with two-factor authentication.


Java: Add Two Numbers Taking Input from User, You can import only the ones you need. This reduces run-time and makes code cleaner. We Duration: 4:50 Posted: Nov 27, 2017 If you don't have the code, in vehicles newer than the 2003 model, your manual will give you instructions as to how to prompt your radio to give you the audio unit serial number. If you have that, a simple call to Honda will get you the code. Otherwise, you'll have to visit a dealer.


While Loops and Input, All of the code in the loop will run as long as the condition remains true. Get some input from the user. variable = input('Please enter a value: ') # Do something with the Have fun!\n") elif choice == '2': print("\nHere are some running shoes. I have the same problem. I tried downgrading tornado and upgrading ipykernel and confirmed that I do not have duplicated nbextensions, but my problem has not been solved yet. (This is pretty annoying and I cannot work on anything.) In my environment, conda list | grep "tornado" gives tornado 5.1.1 py36h1de35cc_0