Java - How do I stop the user from entering something other than an integer?

牧云@^-^@ 提交于 2019-12-11 11:22:23

问题


I've got the program working fine, other the fact that the user can still enter strings/chars that will break the program, causing an error.

I want to write a statement so that when the user inputs anything other than an integer between 0-100 an error message will be displayed (basically my string saying they can't do that) or preferably they wont be able to enter anything besides an integer at all (if possible).

Here is my code:

    int[] cw_Mark = new int[6];
    for (int i = 0; i < cw_Mark.length; i++) {
        if (i >= 0 || i <= 100) {
            System.out.println("Please Enter Your Module " + (i+1) + " Coursework Mark: ");
            cw_Mark[i] = input.nextInt();
        } else {
            System.out.println("Please Enter a Number Between 1 and 100");
        }
    }

回答1:


try this

    int[] cw_Mark = new int[6];
    for (int i = 0; i < cw_Mark.length; i++) {
        System.out.println("Please Enter Your Module " + (i + 1) + " Coursework Mark: ");
        while (true) {
            String next = input.next();
            try {
                cw_Mark[i] = Integer.parseInt(next);
                if (i >= 0 || i <= 100) {
                    break;
                }
            } catch (NumberFormatException e) {
            }
            System.out.println("Please Enter a Number Between 1 and 100");
        }
    }



回答2:


Encapsulate it in a method - something like below.. And then just use the method to fetch the integers from the user.

public static int getInt() {

    System.out.print("Please enter an integer");
    if (console.hasNext("stop")) {
        System.out.println("***Terminated***");
        System.exit(0);
    }
    while(!console.hasNextInt()) {
        System.out.print("Input is not a valid integer!");
        console.next();
    }
    int temp = console.nextInt();
    while(temp<0 && temp>100) {
        System.out.println("invalid input");
        temp = console.nextInt();
    }
    return temp;    
}

EDIT:

This is the entire solution, if going with this approach intead of a try-catch block.

package standard;

import java.util.Scanner;
public class Main {
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args) {

        int[] cw_Mark = new int[6];
        for (int i = 0; i < cw_Mark.length; i++) {
            System.out.println("Please Enter Your Module " + (i + 1)
                    + " Coursework Mark: ");
            cw_Mark[i] = getInt();
        }
    }

    //Gets the next integer from the console. 
    public static int getInt() {

        System.out.print("Please enter an integer");
        if (console.hasNext("stop")) {
            System.out.println("***Terminated***");
            System.exit(0);
        }
        while (!console.hasNextInt()) {
            System.out.print("Input is not an integer!");
            console.next();
        }
        int temp = console.nextInt();
        while (temp <= 0 && temp >= 100) {
            System.out.println("Input must be between 0 and 100");
            temp = console.nextInt();
        }
        return temp;
    }

}



回答3:


To avoid the program to exit throwing an exception, you can simply wrap the user input accepting statement using try/catch:

try {
    cw_Mark[i] = input.nextInt();
} catch(InputMismatchException e) {
     System.out.println("Entered value is not a number");
}



回答4:


This should do it:

while(true)
{
    try {
        System.out.println("Please Enter Your Module " + (i+1) + " Coursework Mark: ");
        cw_Mark[i] = input.nextInt();
        if(cw_Mark[i}>=0&&cw_Mark[i]<=100)
            break;
    } catch(InputMismatchException e) {
         System.out.println("Needs a number.");
    }
    System.out.println("Needs a number from 1 to 100");
}

This will catch any bad values and keep prompting your user to give you proper values until they do.

Edit: To clarify, you just need to paste this over the spot where you currently have

System.out.println("Please Enter Your Module " + (i+1) + " Coursework Mark: ");
        cw_Mark[i] = input.nextInt();

And take out your check for 0 - 100. It's not doing any good there, it doesn't check at the right spot to make a difference.



来源:https://stackoverflow.com/questions/19818269/java-how-do-i-stop-the-user-from-entering-something-other-than-an-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!