How to insist that a users input is an int?

前端 未结 7 1973
日久生厌
日久生厌 2020-12-10 10:09

Basic problem here.. I will start off by asking that you please not respond with any code, as that likely will only confuse me further (programming noob). I am looking for

相关标签:
7条回答
  • 2020-12-10 10:25

    Use scan.hasNextInt() to make sure the next input is an int.

    0 讨论(0)
  • 2020-12-10 10:31

    Without any code and just in English, I'd say there's two things you have to test or look out for. First that the input is an int, second that the int is within the correct range.

    In terms of pseudocode, the first thing to do is make sure it's an int. Declaring an int named "input", I would put a try / catch block, where you try to scan in the user input as an int, with parseInt(). If the try part fails, you know it's not an int and can return an error message.

    Then, now that you know that "input" is an int, you can test whether it is less than 1 or more than 150, and return an error message if so!

    0 讨论(0)
  • Just get "anything" and parse it:

    Scanner scan = new Scanner(System.in);
    
    Integer number = null;
    while (number == null) {
        try {
            number = Integer.parseInt(scan.next());
        } catch (NumberParseException e) {
            System.out.println("bad input: " + input);
        }
    }
    
    0 讨论(0)
  • 2020-12-10 10:42

    I have written an example that ensures that the program will continue only if a number and not an invalid value is entered. Do not worry, I added the desired explanation.

    The program asks the user to input a number. A loop ensures that the processing will not go on until a valid number is entered. Before that I have defined a variable "inputAccepted" that has false as default value. If he enters a number, the variable "inputAccepted" is set to true and the program leaves the loop. But if he enters something else than a number, an exception is thrown right in this moment, and the line that sets the variable "inputAccepted" to true will not be executed. Instead a message will be printed out that tells the user that his input is not valid. Since "inputAccepted" could not be set to true, the loop will do the same stuff again until the string can be converted to a number.

    You can test the program here.

    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            boolean inputAccepted = false;
            while (!inputAccepted) {
                try {
                    System.out.print("Please enter a number: ");
                    Integer.valueOf(input.nextLine());
                    inputAccepted = true;
                } catch (NumberFormatException e) {
                    System.out.println("Not a valid number.");
                }
            }
            System.out.println("Thank you!");
        }
    }
    
    0 讨论(0)
  • 2020-12-10 10:43

    public class Sample {

    /**
     * author CLRZ
     */
    public static void main(String[] args) {
        int a; // variable
        Scanner in = new Scanner(System.in); // scans your input
        System.out.println("Enter your number's choice:"); 
        int sem1 = in.nextInt(); // reads next integer
        if (sem1 == 1) // conditioned if your choice number is equal to 1
        System.out.println("Hello World1"); // output wil be Hello World
        int b;
    
        System.out.println("Enter your number's choice:");
        int sem2 = in.nextInt(); 
        if (sem2 == 2)
        System.out.println("Hello World2");
        int c;
    
        System.out.println("Enter your number's choice:");
        int sem3 = in.nextInt(); 
        if (sem3 == 3)
        System.out.println("Hello World3");
    }
    

    }

    0 讨论(0)
  • 2020-12-10 10:44

    Scanner.hasNextInt() returns true if the next token is a number, returns false otherwise.

    In this example, I call hasNextInt(). If it returns true, I go past the while and set the input; if it returns false, then I discard the input (scanner.next();) and repeat.

    Scanner scan = new Scanner(System.in);
    while(!scan.hasNextInt()) {
        scan.next();
    }
    int input = scan.nextInt();
    
    0 讨论(0)
提交回复
热议问题