[英]Java - read file into array of objects
I am in need of some guidance.我需要一些指导。 I am not sure how to go about reading in the sample text file into an array of objects.
我不确定如何将示例文本文件读入对象数组。 I know that the work needs to be in the while loop I have in main.
我知道这项工作需要在我在 main 中的 while 循环中进行。 I just do not know what I need to accomplish this.
我只是不知道我需要什么来完成这个。
I understand that I need to read in the file line by line (that's what the while loop is doing), but I don't know how to parse that line into an object in my array.我知道我需要逐行读取文件(这就是 while 循环所做的),但我不知道如何将该行解析为数组中的对象。
I know all of you like to see what people have tried before you help, but I honestly don't know what to try.我知道你们所有人都喜欢在提供帮助之前先看看人们尝试过什么,但老实说我不知道该尝试什么。 I don't need a hand out, just some guidance.
我不需要施舍,只需要一些指导。
Sample Text File:示例文本文件:
100 3
120 5
646 7
224 9
761 4
Main:主要的:
public static void main(String[] args) {
Weight[] arrWeights = new Weight[25];
int count = 0;
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println(selectedFile.getAbsolutePath());
BufferedReader inputStream = null;
String fileLine;
inputStream = new BufferedReader(new FileReader(selectedFile.getAbsoluteFile()));
System.out.println("Weights:");
// Read one Line using BufferedReader
while ((fileLine = inputStream.readLine()) != null) {
count++;
System.out.println(fileLine);
}
System.out.println("Total entries: " + count);
}
}
Weight Class:体重等级:
public class Weight {
private int pounds;
private double ounces;
private final int OUNCES_IN_POUNDS = 16;
public Weight(int pounds, double ounces) {
this.pounds = pounds;
this.ounces = ounces;
}
public boolean lessThan(Weight weight) {
return toOunces() < weight.toOunces();
}
public void addTo(Weight weight) {
this.ounces += weight.toOunces();
normalize();
}
public void divide(int divisor) {
if (divisor != 0) {
this.ounces = (this.toOunces() / divisor);
this.pounds = 0;
normalize();
}
}
public String toString() {
return this.pounds + " lbs " + String.format("%.3f", this.ounces) + " oz";
}
private double toOunces() {
return this.pounds * OUNCES_IN_POUNDS + this.ounces;
}
private void normalize() {
if (ounces >=16) {
this.pounds += (int) (this.ounces /OUNCES_IN_POUNDS);
this.ounces = this.ounces % OUNCES_IN_POUNDS;
}
}
}
I don't remember how to do that exactly in Java but I think this general guidance could help you:我不记得如何在 Java 中完全做到这一点,但我认为这个一般指南可以帮助你:
In the while loop -在 while 循环中 -
Parse the line you input from the read line using split function, you can use this as reference( first example can do the trick ): http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html使用 split 函数解析您从读取行输入的行,您可以将其用作参考(第一个示例可以解决问题):http: //pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString .html
Take the parsed line values, cast them to desired values per your class and create your object.获取解析后的行值,将它们转换为每个类所需的值并创建对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.