作业:如何对来自文件的字符串数组列表进行排序

时间:2018-10-25 20:19:29

标签: java arrays string object stringtokenizer

我需要编写一个程序,该程序从文件中读取以下信息。这些列是:以1/100秒为单位的时间滴答声,音符编号,力度,长度。每行都是不同的注释,我需要编写一个程序来读取它们,然后再次将它们打印出来。我想为Note提供一个公共类,以及它们的数组。

0 60 100 24
25 72 100 24
100 60 100 24
50 60 100 24
75 72 100 24

这是我的新Note课

公共类Note实现Comparable {

private int time;
private int noteNumber;
private int velocity;
private int length;

public Note (int time, int noteNumber, int velocity, int length){

    this.time = time;
    this.noteNumber = noteNumber;
    this.velocity = velocity;
    this.length = length;

}


public String toString (){

    return String.format("(%s, %d, %d, %d)", time, noteNumber, velocity, length);
}

@Override
public int compareTo(Note note) {

    return this.time - note.time;
}

}

这是我用来从文件中读取行的类。我只能正确存储第一行,但是现在在尝试存储其他行时遇到了问题。

公共类MelodyCatcher {

公共静态注释[] n =新注释[5];

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

FileReader fr = new FileReader ("/Users/enricomomo/Desktop/Text/file1.txt");
    BufferedReader br = new BufferedReader (fr); //Info stored into a buffer

    String ln = null;
    while ((ln = br.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(ln);
    while(st.hasMoreTokens()) // read each number in this line
    {

        n[0] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
        n[1] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
        n[2] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
        n[3] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
        n[4] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));

    }

    System.out.println("The notes are " + Arrays.toString(n));
    Arrays.sort(n);
    System.out.println("The notes stored are " + Arrays.toString(n));
}

br.close();
fr.close();

} }

1 个答案:

答案 0 :(得分:1)

因此,如果您的行如示例所示,则可以将拆分线简单地分割为Note对象,如下所示:

String[] noteParams = ln.split(" ");
Note n = new Note(noteParams[0], noteParams[1], noteParams[2], noteParams[3]);

我可以按以下方式更改您的代码以读取每一行并填充Note数组:

    String ln = null;
    while ((ln = br.readLine()) != null) {
        StringTokenizer st = new StringTokenizer(ln);
        while(st.hasMoreTokens()) // read each number in this line
        {

            n[0] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
            n[1] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
            n[2] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
            n[3] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
            n[4] = new Note(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));

        }

        System.out.println("The notes are " + Arrays.toString(n));
        Arrays.sort(n);
        System.out.println("The notes stored are " + Arrays.toString(n));
    }

    br.close();
    fr.close();