0

MP3 のリストを実行時間順に並べ替えるために使用Collections.sort()しています。実行時間が等しい場合はタイトルのアルファベット順に並べ替え、タイトルが等しい場合は作曲者順に並べ替えます。stdin入力をieに入れました

入力例

3
&
ピンク・フロスト&フィリップス、マーティン&234933
続編 ゲリエ・イオ・フォッシ&プッチーニ, ジャコモ&297539
ノンピウアンドライ&モーツァルト&234933
M'appari tutt'amor&Flotow、F&252905

しかし、その後、入力を介して何も思いつきません。完全に機能するはずなので、混乱しています。エラーは発生しません。

public class Lab3Algorithm {

public static void main(String args[]) throws IOException {

    List<Song> songs = new ArrayList<Song>();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int numOfSongsRequired = Integer.parseInt(br.readLine());
    String sep = br.readLine();

    String line;
    while((line = br.readLine()) != null) {
        String[] fields = sep.split(line);
        songs.add(new Song(fields[0], fields[1], fields[2]));
    }
    Collections.sort(songs);

    System.out.println(songs);
}

}

public class Song implements Comparable<Song> {

private final String title;
private final String composer;
private final int runningTime;

public Song(String title, String composer, String runningTime) {
    this.title = title;
    this.composer = composer;
    this.runningTime = Integer.parseInt(runningTime);
}

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

public String getComposer(){
    return this.composer;
}

public int getRunningTime(){
    return this.runningTime;
}

@Override
public int compareTo(Song s) {
    if (runningTime > s.runningTime) {
        return 1;
    } else if (runningTime < s.runningTime) {
        return -1;
    }
    int lastCmp = title.compareTo(s.title);
    return (lastCmp != 0 ? lastCmp : composer.compareTo(s.composer));
}
}

誰かが私を正しい方向に向けることができれば、それは私に感謝します.

4

2 に答える 2

3

String[] fields = sep.split(line);間違っているようです-曲の入力で区切り文字列を分割しようとしていません。セパレーターで曲の入力を分割したい:

String[] fields = line.split(sep);
于 2011-05-28T16:20:04.150 に答える
2

あなたの時間は間違っています

for(int i = 0;i<numOfSongsRequired ;i++) {
    line = br.readLine();
    String[] fields = line.split(sep);//line should be splitted by sep btw
    songs.add(new Song(fields[0], fields[1], fields[2]));
}

readline は、EOF がある場合にのみ null を返します (プラットフォームに応じて ctrl-z または ctrl-d)。

それ以外の場合は、次の行で待機するだけでブロックされます

于 2011-05-28T16:20:06.743 に答える