2016-11-26 102 views
0

您好我更新編程,所以我需要一些幫助。我需要制定一種方法來檢查生日是否正確。例如960214,其中96是02年是月,14是日,但生日是一個字符串。因此,這裏是我得到:Character.IsDigit在整數Java中返回false而不是true

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

現在我停在Character.isDigit(年),因爲它返回false,它應該返回true。我打印一年只是爲了看看什麼出來,96出來就像在頂部的例子。我究竟做錯了什麼?

+1

你有沒有*閱讀* [Charact的javadoc er.isDigit(int codePoint)'](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isDigit-int-)? *確定**指定的字符(Unicode代碼點)**是否是數字*。 'year'是一個數字,而不是一個Unicode代碼點。 – Andreas

+0

就像我說過的,我對編程更新,我讀了一些東西,它說「如果字符是數字,則爲true;否則爲false」。是不是一個數字? – Sevajper

+0

我甚至無法想象你認爲'isDigit()'會爲數字做些什麼?一個數字如何可以不是數字?此外,您只是自己說過:*「如果**字符**是數字,則爲true;否則爲false」* – Andreas

回答

2

Character.isDigit(year)除了字符不是數字。 Character.isDigit('5')這將返回true。

+0

但是,這是關於字符Character.isDigit(int CodePoint)你可以簡單地向我解釋這是什麼? – Sevajper

+0

看到這個鏈接: http://www.tutorialspoint.com/java/character_isdigit.htm –

0
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!

0

注:

如果我理解正確的,你要確保所輸入的字符串是一個數字。如果這是問題,應該使用下面的代碼。

建議:

你應該檢查它是否是解析整型,因爲如果立即解析它,它是無效的,這將導致異常前的數字。

代碼:

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!