validation一个整数并使其成为5位数

我正在学习我的第一个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  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开始的邮政编码,您需要将邮政编码存储在字符串中,然后使用正则表达式对其进行validation最简单:

 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. 检查String是否长5个字符(以匹配5个数字)
  2. String不包含+符号,因为+1234可以使用
  3. 检查String是否是有效整数
  4. 检查Integer是否为正,因为-1234仍然有效
  5. 你现在有00000到99999之间的东西

在实践中

 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); } 

邮政编码中存在前导零的问题。 需要检查两者是否为数字,长度为5个字符。 如果作为数字类型读入,则零前导zip将是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 

我使用nextLine()而不是int将输入作为字符串,因为它考虑了以0开头的邮政编码,而邮政编码虽然以数字方式编写,但实际上并不是数值。 我觉得构造if / else语句确定邮政编码是否有效的最简单方法是使用返回语句,这些语句会在返回时突破检查,所以我写了一个方法来检查zip的有效性码:

 public static boolean checkValidZip(String zip) { if (zip.length() != 5) { //invalid if not 5 chars long return false; } for (int i=0; i 

然后,主要方法如下所示:

 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 }