gpt4 book ai didi

java - 作业: How to sort an Array List of String coming from a file

转载 作者:行者123 更新时间:2023-12-02 10:39:52 25 4
gpt4 key购买 nike

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

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

这是我的新 Note 类

公共(public)类 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;
}

}

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

公共(public)课 MelodyCatcher{

public static Note[] n = new Note[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();

}}

最佳答案

因此,如果您的线条如示例所示,您可以简单地将线条分割为注释对象,如下所示:

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

我可以更改您的代码如下,以读取每一行并填充注释数组:

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

关于java - 作业: How to sort an Array List of String coming from a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52997477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com