3

Song オブジェクトの配列を作成しました。曲には、変数 title、author、interpreter、(int)yearReleased、album、および fileName が含まれます。メイン メソッドを使用して、曲の配列と equals メソッドをテストしています。このテストでは、配列に 5 つの曲オブジェクトを入力し、equals メソッドを使用して、新しいエントリが以前のエントリと重複していないことを確認する必要があります。テスト クラスはコンパイルされますが、重複する曲情報を入力するとエラーが発生し続けます。誰かが私にヒントを与えたり、正しい方向に私を向けることができれば、私はそれを大いに感謝します. 他のヒントも素晴らしいでしょう。学生として、専門家から実際のアドバイスを聞くことは素晴らしいことです。

 import java.util.Scanner;

 public class Test{
 public static void main(String[] args)
 {
    Scanner kybd = new Scanner(System.in);

    Song[] songTest = new Song[5];
    boolean match;
    int count = 0;

    System.out.println("Enter 5 songs\n");

    for(Song x:songTest)
    {
        do{
            match = false;
            x = new Song();
            System.out.print("Title: ");
            x.setTitle(kybd.nextLine());
            System.out.print("Author: ");
            x.setAuthor(kybd.nextLine());
            System.out.print("Interpreter: ");
            x.setInterpreter(kybd.nextLine());
            System.out.print("Year released: ");
            x.setYearReleased(kybd.nextInt());
            kybd.nextLine();
            System.out.print("Album: ");
            x.setAlbum(kybd.nextLine());
            System.out.print("File name: ");
            x.setFileName(kybd.nextLine());
            System.out.print(x);
            System.out.println();
            for(int i = 0; i<count; i++)
                if(songTest[i].equals(x)){
                    match = true;
                    System.out.print("Duplicate");
                }
        }while(match);
        count++;
    }
}
}




public class Song
{
public String title;
public String author;
public String interpreter;
public int yearReleased;
public String album;
public String fileName;

//private vars
private int reviewScore = 0;
private int reviews = 0;
private double average;

//Mutator methods

public void setTitle(String t)
{
    this.title = t;
}

public void setAuthor(String a)
{
    this.author = a;
}

public void setInterpreter(String i)
{
    this.interpreter = i;
}

public void setYearReleased(int y)
{
    if (y>0)
        this.yearReleased = y;
    else
    {
        System.out.print ("This song is not that old");
        this.yearReleased = -5;
    }
}

public void setAlbum(String a)
{
    this.album = a;
}

public void setFileName(String f)
{
    this.fileName = f;
}

public void addReviewScore(int s)
{
    if (s>0 && s<6)
    {
        this.reviewScore += s;
        this.reviews++;
    }
    else
        System.out.print("This is not a valid review score!");
}

//Accessor methods

public String getTitle()
{
    return this.title;
}

public String getAuthor()
{
    return this.author;
}

public String getInterpreter()
{
    return this.interpreter;
}

public int getYearReleased()
{
    return this.yearReleased;
}

public String getAlbum()
{
    return this.album;
}

public String getFileName()
{
    return this.fileName;
}

public double getAverage()
{
    this.average = this.calculateAverage();
    return this.average;
}

//Methods

public boolean equals(Song otherSong)
{
    boolean isEqual = false;
    //compare this song to the otherSong
    isEqual =
        this.title == otherSong.title &&
        this.author == otherSong.author &&
        this.interpreter == otherSong.interpreter &&
        this.yearReleased == otherSong.yearReleased &&
        this.album == otherSong.album &&
        this.fileName == otherSong.fileName;
    return isEqual;
}
public String toString()
{
    String songInfo;
    songInfo = 
        "***Song information***\n" +
        "Title: " + this.title + 
        "\nAuthor: " + this.author +
        "\nInterpreter: " + this.interpreter +
        "\nYear Released: " + this.yearReleased +
        "\nAlbum: " + this.album +
        "\nFile name: " + this.fileName +
        "\nYears old: " + this.yearsOld(); 
    return songInfo;
}

public int yearsOld()
{
    int yearsOld = (2012 - this.yearReleased);
    return yearsOld;
}

//Private methods
private double calculateAverage()
{
    this.average = ((double)this.reviewScore/(double)this.reviews);
    return this.average;

}

}

4

2 に答える 2

2

私はあなたのコードを試し、この行で発生するエラーを再現しました:

                if(songTest[i].equals(x)){

あなたのequalsメソッドを書き直して(またはEclipseにそれをやってもらい)、hashCode()を追加すると問題が解決しました:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((album == null) ? 0 : album.hashCode());
    result = prime * result + ((author == null) ? 0 : author.hashCode());
    long temp;
    temp = Double.doubleToLongBits(average);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    result = prime * result
            + ((fileName == null) ? 0 : fileName.hashCode());
    result = prime * result
            + ((interpreter == null) ? 0 : interpreter.hashCode());
    result = prime * result + reviewScore;
    result = prime * result + reviews;
    result = prime * result + ((title == null) ? 0 : title.hashCode());
    result = prime * result + yearReleased;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Song other = (Song) obj;
    if (album == null) {
        if (other.album != null)
            return false;
    } else if (!album.equals(other.album))
        return false;
    if (author == null) {
        if (other.author != null)
            return false;
    } else if (!author.equals(other.author))
        return false;
    if (Double.doubleToLongBits(average) != Double
            .doubleToLongBits(other.average))
        return false;
    if (fileName == null) {
        if (other.fileName != null)
            return false;
    } else if (!fileName.equals(other.fileName))
        return false;
    if (interpreter == null) {
        if (other.interpreter != null)
            return false;
    } else if (!interpreter.equals(other.interpreter))
        return false;
    if (reviewScore != other.reviewScore)
        return false;
    if (reviews != other.reviews)
        return false;
    if (title == null) {
        if (other.title != null)
            return false;
    } else if (!title.equals(other.title))
        return false;
    if (yearReleased != other.yearReleased)
        return false;
    return true;
}

あなたもカウンターが適切に増加しないという問題を抱えているようですが、私はあなたのために宿題をするつもりはありません! ;)

編集:うわー!次のようなものも欠けています

       songTest[i] = song;

チェックした曲を配列に追加します。

また、最初の曲が確実に挿入されるように、次のように追加しました。

if(i==0){
        songTest[i] = x;
}

あなたのチェックの前に。最初の for ループを昔ながらのバージョンに戻して追加した i is と int を、内部の for ループの名前を j に変更しました。今では動作します。次のようなものを入れてみてください:

System.out.println("i: " + i + " j: " + j + " count: " + count);

カウンターで何が起こっているかを確認するには

重複を見つけた後も終了します。これはあなたが望む動作ですか?それとも、ユーザーに通知して、曲データの入力を続けた方がよいでしょうか。

于 2012-09-26T02:03:39.710 に答える
1

これを実行すると、この行でnullポインター例外が発生します

if(songTest[i].equals(x)){

曲オブジェクト(x)を実際に配列に入れていないようです。

于 2012-09-26T02:00:34.673 に答える