[英]Java: Using .txt files to assign specific indexes of each line to an arraylist
我的.txt文件是
code1,description1,price1
code2,description2,price2
etc.
使用方法:
ArrayList<String> items = new ArrayList<String>();
String description;
File fn = new File("file.txt");
String[] astring = new String[4];
try{
Scanner readFile = new Scanner(fn);
Scanner as = new Scanner(System.in);
while (readFile.hasNext()){
astring = readFile.nextLine().split(",");
String code = astring[0];
items.add(code);
description = astring[1];
}
}catch(FileNotFoundException){
//
}
for(String things: items){
System.out.println("The code is: " + things + "The description is " + description);
}
我的輸出打印出來
code1 description1
code2 description1
code3 description1
我正在嘗試找出如何像代碼一樣更新描述。 例如
code1 description1
code2 description2
code3 description3
如果已經問過這個問題,我深表歉意。 我無法通過搜索來找到方法,但是如果有參考可以解決這個問題,那么我將關閉它並去那里。 提前致謝!
您看到輸出的原因是您沒有將描述和列表中的代碼一起保存,這就是為什么最后一個描述保存在description變量中而不是所有描述值的原因。
為了解決這個問題,您可以創建一個簡單的Java Bean / POJO類並將數據包裝在其中,然后可以簡單地獲取保存的值然后正確顯示它。 看一下下面的代碼:
public class Launcher{
public static void main(String[] args) {
ArrayList<Item> items = new ArrayList<Item>();
File fn = new File("file.txt");
try {
Scanner readFile = new Scanner(fn);
while (readFile.hasNext()) {
String[] astring = readFile.nextLine().split(",");
String code = astring[0];
String description = astring[1];
String price = astring[2];
Item item = new Item(code, description, price);
items.add(item);
}
} catch (FileNotFoundException d) { // }
}
for (Item thing : items) {
System.out.println(String.format("The code is: %s\tThe description is: %s\tThe Price is %s",thing.getCode(),thing.getDescription(), thing.getPrice()));
}
}
}
class Item {
private String code;
private String description;
private String price;
public Item(String code, String description, String price) {
this.code = code;
this.description = description;
this.price = price;
}
public String getCode() {
return code;
}
public String getDescription() {
return description;
}
public String getPrice() {
return price;
}
}
問題出在你的邏輯上。 您僅將astring[0]
存儲到ArrayList items
並且每次都覆蓋description
的值。 結果,最后讀取的值存儲在要在循環中打印的description
中。
我更喜歡如下創建一個自定義類。 (僅出於演示目的,否則您將聲明您的字段為私有字段並提供getter和setter方法)
class MyObject {
public String code;
public String description;
public String price;
}
現在,您將創建MyObject的ArrayList而不是創建String的ArrayList,如下所示
ArrayList<MyObject> items = new ArrayList<MyObject>();
現在,每當您讀取一行時,就創建一個MyObject的新實例,用astring
的值填充其字段,如下所示
ArrayList<MyObject> items = new ArrayList<MyObject>();
File fn = new File("test.txt");
String[] astring = new String[4];
try {
Scanner readFile = new Scanner(fn);
Scanner as = new Scanner(System.in);
MyObject myObject;
while (readFile.hasNext()) {
astring = readFile.nextLine().split(",");
myObject = new MyObject();
myObject.code = astring[0];
myObject.description = astring[1];
myObject.price = astring[2];
items.add(myObject);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
然后最后使用相同的foreach循環將其打印如下
for (MyObject item : items) {
System.out.println("The code is: " + item.code + " The description is: " + item.description + " The price is: " + item.price);
}
輸出量
The code is: code1 The description is: description1 The price is: price1
The code is: code2 The description is: description2 The price is: price2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.