[英]How to check if an int contains a letter
I am trying to validate my code by error checking. 我正在尝试通过错误检查来验证我的代码。 I want to make sure the integer people enter does not contain a letter or more.
我想确保输入的整数不包含字母或更多。 Here is my code.
这是我的代码。 I am supposed to solve this problem using a one dimensional array.
我应该使用一维数组解决此问题。 I got the code working but I am having problems with adding the error checking in. Any help would be appreciated.
我可以使用代码,但是在添加错误检查时遇到了问题。我们将不胜感激。 Thanks
谢谢
public void getNumbers() {
Scanner keyboard = new Scanner(System.in);
int array[] = new int[5];
int count = 0;
int entered = 0;
int k = -1;
while (entered < array.length) {
System.out.print("Enter a number ");
int number = keyboard.nextInt();
if (10 <= number && number <= 100) {
boolean containsNumber = false;
entered++;
for (int i = 0; i < count; i++) {
if (number == array[i]) // i Or j
{
containsNumber = true;
}
}
if (!containsNumber) {
array[count] = number;
count++;
} else {
System.out.println(number + " has already been entered");
}
} else {
System.out.println("number must be between 10 and 100");
}
//what does %d do?
for (int j = 0; j < count; j++) {
System.out.printf("%d ", array[j]);
}
System.out.println();
}
}
} }
I'm assuming that you would want your program to ask the user to re-enter a number if they do not input a number the first time. 我假设您希望程序在用户第一次不输入数字时要求用户重新输入数字。 In this scenario you might want to try something along the lines of this:
在这种情况下,您可能需要尝试以下方法:
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
while(!sc.hasNextInt()) {
//print some error statement
sc.nextLine();
}
int number = sc.nextInt();
System.out.println("Number is: " + number); // to show the value of number
// continue using number however you wish
Since hasNextInt()
returns a boolean determining whether or not the input is an Integer, the program will never leave the while-loop until the program can confirm that the user has entered an integer. 由于
hasNextInt()
返回一个布尔值,确定输入是否为整数,所以程序将永远不会离开while循环,直到程序可以确认用户输入了整数为止。
keyboard.nextInt()
will throw a InputMismatchException
if you input a String. 如果您输入String,则
keyboard.nextInt()
将引发InputMismatchException
。
If you want to check whether Scanner
has an integer to read, you can use keyboard.hasNextInt()
. 如果要检查
Scanner
是否具有要读取的整数,则可以使用keyboard.hasNextInt()
。
Alternatively, you can read the input as String s = keyboard.next()
which will take the input as a String, and then use s.matches(".*\\\\d+.*")
to detect whether or not it is an integer. 或者,您可以将输入读取为
String s = keyboard.next()
,它将输入作为字符串,然后使用s.matches(".*\\\\d+.*")
来确定它是否为整数。
UPDATE: To answer questions - keyboard.hasNextInt()
will return a boolean. 更新:要回答问题
keyboard.hasNextInt()
将返回一个布尔值。 So for example, after System.out.print("Enter a number")
, you could have an if
statement checking to see if keyboard
can receive numerical input, ie. 因此,例如,在
System.out.print("Enter a number")
,您可以使用if
语句检查keyboard
可以接收数字输入,即。 if(keyboard.hasNextInt)
. if(keyboard.hasNextInt)
。 If this is true, that means the user has entered numerical input, and you could continue with saying int number = keyboard.nextInt()
. 如果为true,则表示用户已输入数字输入,您可以继续说
int number = keyboard.nextInt()
。 If it is false, you would know that the user input is non-numerical. 如果为假,则您将知道用户输入是非数字的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.