我正在学习我的第一个java课程。我需要索要一个邮政编码。我知道如果不输入5位数,如何要求新输入,但如果他们输入非整数,我如何要求新输入?
这就是我所拥有的:
import java.util.Scanner;
public class AndrewDemographics {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int zip; // 5 digit zip
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.nextInt();
while ((zip < 10000) || (zip > 99999)) {
// error message
System.out.println("Invalid Zip Code format.");
System.out.println("");
System.out.println("Enter your 5 digit zip code: ");
zip = stdIn.nextInt();
} //end if zip code is valid
}
}
答案 0 :(得分:4)
要支持以0
开头的邮政编码,您需要将邮政编码存储在字符串中,然后使用正则表达式对其进行验证最简单:
Scanner stdIn = new Scanner(System.in);
String zip;
do {
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.next();
} while (! zip.matches("[0-9]{5}"));
如果你想打印错误信息,你可以这样做,使用nextLine()
,所以只需按Enter键也会打印错误信息:
Scanner stdIn = new Scanner(System.in);
String zip;
for (;;) {
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.nextLine().trim();
if (zip.matches("[0-9]{5}"))
break;
System.out.println("Invalid Zip Code format.");
System.out.println();
}
答案 1 :(得分:2)
如评论所示,您需要考虑从零开始的邮政编码。我想为此,你需要将输入视为字符串:
String
是否长5个字符(以匹配5位数字)String
不包含+
符号,+1234
可以使用String
是否为有效整数Integer
是否为正,因为-1234
仍然有效在实践中
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
String userInput;
int zipCode = -1;
// flag to stop spamming the user
boolean isValid = false;
while (!isValid) {
// ask the user
System.out.print("Enter your 5 digit zip code: ");
userInput = stdIn.next();
// it should be 5 digits so 5 charaters long:
if (userInput.length() == 5 && !userInput.contains("+")) {
try {
zipCode = Integer.parseInt(userInput);
if (zipCode > 0) {
isValid = true;
}
}
catch (NumberFormatException e) {
// do nothing
}
}
System.out.println("Zip code is invalid!");
}
System.out.println("You have selected the zip code: " + zipCode);
}
答案 2 :(得分:0)
以前使用前导零的邮政编码存在问题。需要检查两者是否为数字,长度为5个字符。如果以数字形式读入,则零前导拉链的长度为4位。
我的头脑:
String zip = null;
do {
zip = stdIn.next();
try {
Integer.parseInt(zip); // this will throw exception if not a number
} catch (NumberFormatException e) {
continue; // this will start the next loop iteration if not a number
}
} while (zip.length() != 5); // this will start the next iteration if not 5 characters
答案 3 :(得分:0)
我使用nextLine()
而不是int将输入作为字符串使用,因为它考虑了以0开头的邮政编码,而邮政编码虽然以数字方式编写,但并不是真正的数值。我觉得构造if / else语句确定邮政编码是否有效的最简单方法是使用会在返回时突破检查的return语句,所以我写了一个方法来检查zip的有效性代码:
public static boolean checkValidZip(String zip) {
if (zip.length() != 5) { //invalid if not 5 chars long
return false;
}
for (int i=0; i<zip.length(); i++) { //invalid if not all digits
if (!Character.isDigit(zip.charAt(i))) {
return false;
}
}
return true; //valid if 5 digits
}
然后,主要方法如下:
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
String zip = ""; //5 digit zip
boolean valid = false;
boolean allNums = true;
while (!valid) {
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.nextLine();
valid = checkValidZip(zip);
if (!valid) {
System.out.println("Invalid Zip Code format.");
System.out.println("");
}
}
//end if zip code valid
}