我的.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
如果已经问过这个问题,我深表歉意。我无法通过搜索来找到方法,但是如果有参考可以解决这个问题,那么我将关闭它并去那里。预先感谢!
答案 0 :(得分:1)
看到该输出的原因是您不将描述以及代码保存在列表中,这就是为什么最后一个描述被保存在说明变量,而不是所有说明值。
要解决此问题,您可以创建一个简单的 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;
}
}
答案 1 :(得分:1)
问题出在你的逻辑上。您仅将astring[0]
存储到items
ArrayList中,并且每次都覆盖description
的值。结果,读取的最后一个值存储在要在循环中打印的description
中。
我更喜欢如下创建一个自定义类。 (仅出于演示目的,否则您将声明字段为私有字段并提供getter和setter方法)
class MyObject {
public String code;
public String description;
public String price;
}
现在,您将创建MyObject的ArrayList,而不是创建字符串的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