简体   繁体   English

在整数Java上,Character.IsDigit返回false而不是true

[英]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;
  }
}

Try it here! 在这里尝试

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.

相关问题 java 方法 Character.isDigit(char) 返回 true,输入为 'O' - The java method Character.isDigit(char) returns true with an input of 'O' Character.isDigit 返回什么? - What does Character.isDigit return? Character.isDigit() 错误:找不到适合 isDigit(String) 的方法 - Character.isDigit() error: no suitable method found for isDigit(String) for循环(记住最后看的位置)和Character.isDigit() - for loop (remember where last to look) & Character.isDigit() Java Hashset返回false而不是true - Java Hashset returns false instead of true isDigit方法中的Character.isDigit参数和行不可用错误(说明) - Character.isDigit parameters and line unavailable errors within the isDigit method (clarification) 使用Character.isLetter和Character.isDigit忽略数字,空格和读取字符串输入 - Ignoring digits, blank spaces and read String input, using Character.isLetter & Character.isDigit 如果整数可被3整除,则该方法返回true;如果整数不能被3整除,则该方法返回false。 - The method returns true if the integer is divisible by 3 and returns false if the integer is not divisible by 3 在java中,是否可以将字符分配为true或false语句? - In java, is it possible to assign a character as a true or false statement? 实际上为true时,该方法返回false - the method returns false when in fact it is true java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM