[英]How to read a text file with multiple data types and store them in an Array?
I am trying to read a text file of various data and getting it stored into an Array, so that after receiving user input I can compare it to see if there are any matches inside the Array.我正在尝试读取各种数据的文本文件并将其存储到数组中,以便在接收到用户输入后我可以比较它以查看数组内是否有任何匹配项。
example text:示例文本:
1A 1st true false false 28.50 free
2B 2nd false true false 25.00 free
3C 3rd true true false 32.50 free
As you can see I'm dealing with Strings, booleans and a double.如您所见,我正在处理字符串、布尔值和双精度值。
I initially tried reading the file and storing each line as a String in a String Array but that didn't work when I then needed to compare the user input.我最初尝试读取文件并将每一行作为字符串存储在字符串数组中,但是当我需要比较用户输入时这不起作用。
So I then tried to create an Array and a sub-array like below:所以我然后尝试创建一个数组和一个子数组,如下所示:
try {
File read = new File("seats.txt");
Scanner reader = new Scanner(read);
while (reader.hasNextLine()) {
String dataRead = reader.nextLine();
String[] seatData = dataRead.split(" ");
String seatID = seatData[0];
String seatClass = seatData[1];
boolean window = Boolean.parseBoolean(seatData[2]);
boolean aisle = Boolean.parseBoolean(seatData[3]);
boolean table = Boolean.parseBoolean(seatData[4]);
double seatPrice = Double.parseDouble(seatData[5]);
String seatFree = seatData[6];
Seats info = new Seats(seatID, seatClass, window, aisle, table, seatPrice, seatFree);
result = info;
System.out.println(result); }
reader.close();
}catch (FileNotFoundException e) {
System.out.println("An error has occurred.");
e.printStackTrace(); }
return result;
}
Is this the right method or should I look at trying something else?这是正确的方法还是我应该尝试其他方法?
Any insight or advice would be much appreciated.任何见解或建议将不胜感激。 Thanks.
谢谢。
You reading and parsing is good, but you're not saving anything, you keep overwriting result
with the last info
, so your method return only one Seats
instance, the last one, use a List
to easily add all the Seats you create您阅读和解析很好,但是您没有保存任何内容,您一直用最后一个
info
覆盖result
,因此您的方法仅返回一个Seats
实例,最后一个,使用List
轻松添加您创建的所有 Seats
With some improvements经过一些改进
try_with-resources
for autoclosing the scanner try_with-resources
用于自动关闭扫描仪
one row is one seat, name the class Seat
一排是一个座位,命名为class
Seat
static List<Seat> read() {
List<Seat> results = new ArrayList<>();
File read = new File("seats.txt");
String dataRead;
try (BufferedReader reader = new BufferedReader(new FileReader(read))) {
while ((dataRead = reader.readLine()) != null) {
String[] seatData = dataRead.split(" ");
String seatID = seatData[0];
String seatClass = seatData[1];
boolean window = Boolean.parseBoolean(seatData[2]);
boolean aisle = Boolean.parseBoolean(seatData[3]);
boolean table = Boolean.parseBoolean(seatData[4]);
double seatPrice = Double.parseDouble(seatData[5]);
String seatFree = seatData[6];
Seat info = new Seat(seatID, seatClass, window, aisle, table, seatPrice, seatFree);
System.out.println(info);
results.add(info);
}
} catch (FileNotFoundException e) {
System.out.println("An error has occurred.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return results;
}
Use Jackson.使用 Jackson。 https://cowtowncoder.medium.com/reading-csv-with-jackson-c4e74a15ddc1 .
https://cowtowncoder.medium.com/reading-csv-with-jackson-c4e74a15ddc1 。 See second last example.
见倒数第二个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.