2016-11-26 6 views
0

こんにちは、私はプログラミングに新しいですので、私はいくつかの助けが必要です。誕生日が正しいかどうかを確認する方法を作る必要があります。例えば960214の場合、96は02年、14は日ですが、誕生日は文字列です。だからここに私が得たものである:それはtrueを返すべきところ、それはfalseを返すのでCharacter.IsDigitは、整数で真ではなく偽を返します。

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(年)で停止。私は何が出てくるのかを見るために年を印刷しました。そして、上の例のように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

私がプログラミングに新しいと言ったように、文字が数字の場合は真、そうでない場合は偽を読んでいます。数字は数字ではありませんか? – Sevajper

+0

isDigit()が数字に対して何をすると思うか想像もできません。数字はどうやって「数字」ではないのですか?また、あなたはちょうどそれをあなた自身が言った:* "**文字**が数字であれば真、そうでなければ偽" * – Andreas

答えて

2

Character.isDigit(年)以外の文字は数字ではありません。 Character.isDigit( '5')これはtrueを返します。

+0

それはCharacter.isDigit(int CodePoint)についてのcharのためのものですが、それは簡単に私に説明できますか? – 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! 
関連する問題