0

私のプログラムは今何をしますか:

  • 次の情報を持つ人物オブジェクトを作成します: firstname, lastname, birthdate(データは別のクラスです)。
  • 日付クラスには、日、月、年、および 18+ (はいまたはいいえ) の 4 つの変数があります。

何が機能するか: , を使用して person オブジェクトを作成でき、成功firstnameしました。lastnamebirthdate

私の人のクラス(うまくいくように見えるもの)。

public class Person {

    public String firstName;
    public String lastName;
    public Date date; 

    public String toString() {
        return (firstName + " " + lastName + " (" + date); 
    }


    public Person(String firstName, String lastName, Date date) {
        this.firstName = firstName; 
        this.lastName = lastName; 
        this.date = date; 
    }

}

メインを含むMYクラス。ここには、自分の人を作成するメソッドもあります。

public static Person setName() {

    String name; 
    String lastName
    String inputBirthdate;
    Date niceDate 
    Date newDate; 


    System.out.println("Firstname:");
    firstName = userInput();  
    System.out.println("Lastname:");
    lastName = userInput(); 
    System.out.println("Birthday:");
    inputBirthdate = userInput(); 
    niceDate = new Date(inputBirthdate);
    newDate = new Date(niceDate);

    return new Gast(firstName, lastName, newDate); 

}

そして、日付の入力が正しいかどうかを確認する Date クラスがあります。4 番目の変数なしで Date クラスを正しく動作させることができることに注意してください。

public class Date {

public String day; 
public String month; 
public String year;
public boolean child; 

public String toString() {
    return (day + "." + month + "." + year + "." + child);
}


/*Date(String day, String month, String year, boolean child) {
    this.day = dag; 
    this.month = month; 
    this.year = year; 
    this.child = child; 
}*/ //don't need this one, output is the same

public Date(Datum niceDate) {
    int bYear = Integer.parseInt(niceDate.year;
    int bMonth = Integer.parseInt(niceDate.day);
    int bDay = Integer.parseInt(niceDate.day);

    boolean child = false; 

    if (bYear > 1995) {
        this.child= true; 
    } else if (bYear == 1995 && bMonth > 10) {
        this.child = true; 
    } else if (bYear == 1995 && bMonth == 10 && bDay > 1) {
        this.child = true; 
    } else {
        this.child = false; 
    }  

}

public Date(String birthdate) {
    String patroon = "\\d{2}-\\d{2}-\\d{4}";
    boolean b = birthdate.matches(patroon);
    if (b) {
        String[] str = birthdate.split("-"); 
        for (String s: str)
        this.day = str[0];
        this.month = str[1];
        this.year = str[2];
    }
    else {
        System.out.println("Birthday is formatted wrong");
    } 
}

}

これを実行すると (大人のチェックを行うかどうか (チェックは機能しているように見えます))、しかし、私の入力birthdateは null を返します:

   Room 1: Name name (null.null.null)false    //boolean works, date not
   Room 2: available

Date問題は、クラスの2番目のメソッドで、public Date(Date Nicedate)日付をintに解析した後に日付を削除することだと思います。

したがって、基本的にはブール値のみを返し、文字列をまったく同じに保ち、計算用の Int として使用するために編集するだけです。

誰かが私を正しい方向に向けることができますか? おそらくそれは非常に単純な解決策ですが、私は一日中それに取り組んできましたが、解決策がわかりません.

必要に応じて編集: (パブリック Date(datum niceDate) でこのステートメントを使用しましたが、日付はまだ表示されません。うーん:

public Date(Datum niceDate) {
        this.year = year; 
        this.day = day; 
        this.month = month; 

    int bYear = Integer.parseInt(niceDate.year;
    int bMonth = Integer.parseInt(niceDate.day);
    int bDay = Integer.parseInt(niceDate.day);

    boolean child = false; 

    if (bYear > 1995) {
        this.child= true; 
    } else if (bYear == 1995 && bMonth > 10) {
        this.child = true; 
    } else if (bYear == 1995 && bMonth == 10 && bDay > 1) {
        this.child = true; 
    } else {
        this.child = false; 
    }  

}

public Date(String birthdate) {
    String patroon = "\\d{2}-\\d{2}-\\d{4}";
    boolean b = birthdate.matches(patroon);
    if (b) {
        String[] str = birthdate.split("-"); 
        for (String s: str)
        this.day = str[0];
        this.month = str[1];
        this.year = str[2];
    }
    else {
        System.out.println("Birthday is formatted wrong");
    } 
}

}

4

2 に答える 2

0
public Date(Datum niceDate) {
    this.year = year; 

このコンストラクターでは、同じメンバー変数this.yearを参照します。yearしたがって、その変数の値をそれ自体に割り当てています。

その後、あなたは

int bYear = Integer.parseInt(niceDate.year);

からの値を解析し、その値を という名前のローカル変数niceDateに割り当てます。代わりに、 toの結果を代入する必要があります:bYearparseIntthis.year

this.year = Integer.parseInt(niceDate.year);

他のすべての変数に同様の変更を加えることができます。

于 2013-10-07T00:26:59.443 に答える