[英]Character.IsDigit returns false instead of true on an integer Java
Hi I am newer to programming so I need some help. 嗨,我是编程新手,所以我需要一些帮助。 I need to make a method where I check if a birthday is correct. 我需要制定一种检查生日是否正确的方法。 For instance 960214 where 96 is year 02 is month and 14 is day, but the birthday is a String. 例如960214,其中96是02年是月份,而14是天,但是生日是字符串。 So here is what I got: 所以这就是我得到的:
private static boolean checkCorrect(String a) {
int year = Integer.parseInt(a.substring(0,2));
int month = Integer.parseInt(a.substring(2, 4));
int day = Integer.parseInt(a.substring(4, 6));
if (a.length() == 6 && Character.isDigit(year)) {
return true;
} else
return false;
}
Now I stopped at Character.isDigit(year) because it returns false where it should return true. 现在我停在Character.isDigit(year),因为它在应该返回true的地方返回false。 I printed year just to see what comes out and 96 comes out just like in the example on top. 我打印年份只是为了看到结果,就像上面的示例一样,结果是96。 What am I doing wrong? 我究竟做错了什么?
Character.isDigit(year) excepts a char not a number. Character.isDigit(year),但不包括数字。 Character.isDigit('5') this will return true. Character.isDigit('5')这将返回true。
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Write your Birthday in the form yymmdd: ");
String date = input.nextLine();
if(checkDate(date))
System.out.println("Your Birthday: " + date + " is valid :)");
else
System.out.println("Your Birthday: " + date + " is invalid :(");
}
public static Boolean checkDate(String date){
if(date.length() != 6) {
System.err.println("Please enter your Birthday in the form yymmdd... ");
System.exit(1);
}
try {
int year = Integer.parseInt(date.substring(0,2));
int month = Integer.parseInt(date.substring(2, 4));
int day = Integer.parseInt(date.substring(4, 6));
if ((00<=year && year<=99) && (01<=month && month<=12) && (01<day && day<=31))
return true;
} catch (NumberFormatException e) {
e.printStackTrace();
}
return false;
}
}
Note: 注意:
If I understand correctly, you want to make sure that the inputted String is a number. 如果我理解正确,则要确保输入的字符串是数字。 The following code should if that is the issue. 如果这是以下问题,则应使用以下代码。
Advice: 忠告:
You should check if it is a number before parsing the Integer, because if you parse it immediately and it is invalid, it will cause an Exception. 您应该在解析Integer之前检查它是否为数字,因为如果立即解析它并且它是无效的,它将导致Exception。
Code: 码:
public static void main(String args[]) {
String input = "960214"; // Sets the value of the String
String year = input.substring(0, 2); // Finds the "year" value in the
// input
Boolean isANumber = true; // The year is thought to be a number unless
// proven otherwise
try {
Integer.parseInt(year); // Tries to parse the year into an integer
} catch (Exception ex) { // Catches an Exception if one occurs (which
// would mean that the year is not an
// integer
isANumber = false; // Sets the value of "isANumber" to false
}
if (isANumber) { // If it is a number...
System.out.println(year + " is a number!"); // Say it is a number
} else {
System.out.println(year + " is not a number!"); // Say it is not a
// number
}
}
Output: 输出:
96 is a number!
Output (When the "input" is "xx0214"): 输出(当“输入”为“ xx0214”时):
xx is not a number!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.