使用 Collections.sort() 对 MP3 列表进行排序

发布于 2024-11-09 23:13:21 字数 1821 浏览 0 评论 0原文

我使用 Collections.sort() 按运行时间顺序对 MP3 列表进行排序,如果运行时间相等,则按标题的字母顺序排序,如果标题相等,则按作曲家排序。我将输入放入 stdin 中,即

示例输入

3
&
Pink Frost&Phillipps, Martin&234933
Se quel guerrier io fossi&Puccini, Giacomo&297539
Non piu andrai&Mozart&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));
}
}

如果有人能给我指出正确的方向,那会让我感激不已。

I'm using Collections.sort() to sort a list of MP3s in order of runtime, and if runtime is equal, then sort alphabetically by title, and if title is equal then sort by composer. I put the input into stdin i.e.

Example Input

3
&
Pink Frost&Phillipps, Martin&234933
Se quel guerrier io fossi&Puccini, Giacomo&297539
Non piu andrai&Mozart&234933
M'appari tutt'amor&Flotow, F&252905

But then it doesn't come up with anything via the input. I am confused since it should work perfectly. I don't get any errors.

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

If someone could point me in the right direction, that would make me grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年 2024-11-16 23:13:21

String[] fields = sep.split(line); 似乎是错误的 - 您没有尝试拆分歌曲输入上的分隔符字符串;您想在分隔符上分割输入的歌曲:

String[] fields = line.split(sep);

String[] fields = sep.split(line); seems wrong - you're not trying to split the separator string on the song input; you want to split the song input on the separator:

String[] fields = line.split(sep);
时光暖心i 2024-11-16 23:13:21

你的 while 是错误的

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 取决于平台),

否则它只会阻止等待下一行

your while is faulty

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 only gives null on if there is a EOF (ctrl-z or ctrl-d depending on the platform)

otherwise it just blocks waiting on the next line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文