有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

字符串。IsDigit在整数Java上返回false而不是true

嗨,我是编程新手,所以我需要一些帮助。如果我需要一个正确的方法来做生日检查。例如,960214,其中96表示年份02表示月份,14表示日期,但生日是一个字符串。这就是我得到的:

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

现在我停在角色上。isDigit(year),因为它在应该返回true的地方返回false。我打印year只是为了看看结果是什么,96出来的结果就像上面的例子一样。我做错了什么


共 (3) 个答案

  1. # 1 楼答案

    注意:

    如果我理解正确,您需要确保输入的字符串是一个数字。如果这是问题所在,则应显示以下代码

    建议:

    在解析整数之前,您应该检查它是否是一个数字,因为如果您立即解析它并且它无效,那么它将导致异常

    代码:

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

    输出:

    96 is a number!
    

    输出(当“输入”为“xx0214”时):

    xx is not a number!
    
  2. # 2 楼答案

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

    试试看here!

  3. # 3 楼答案

    性格。isDigit(year)只包含字符而不是数字。 性格isDigit('5')这将返回true