Java: 使用 .txt 文件将每行的特定索引分配给 arraylist
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
如果已经有人问过这个问题,我深表歉意。我无法通过搜索找到如何做到这一点,但如果有参考可以解决这个问题,我会关闭它并去那里。提前致谢!
您看到该输出的原因是您没有将描述连同代码保存在列表中,也就是说为什么最后一个描述保存在描述变量中而不是所有描述值中。
To solve this problem, you can create a simple Java Bean/POJO class
and wrap your data inside it and then you can simply fetch the value
you have saved it and then show it properly. Take a look at the code
below:
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]
存储到 items
ArrayList 并每次覆盖 description
的值。结果,最后读取的值存储在您在循环中打印的 description
中。
我更喜欢按如下方式创建自定义 class。 (只是为了演示,否则你会将你的字段声明为私有并提供 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
我的 .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
如果已经有人问过这个问题,我深表歉意。我无法通过搜索找到如何做到这一点,但如果有参考可以解决这个问题,我会关闭它并去那里。提前致谢!
您看到该输出的原因是您没有将描述连同代码保存在列表中,也就是说为什么最后一个描述保存在描述变量中而不是所有描述值中。
To solve this problem, you can create a simple Java Bean/POJO class and wrap your data inside it and then you can simply fetch the value you have saved it and then show it properly. Take a look at the code below:
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]
存储到 items
ArrayList 并每次覆盖 description
的值。结果,最后读取的值存储在您在循环中打印的 description
中。
我更喜欢按如下方式创建自定义 class。 (只是为了演示,否则你会将你的字段声明为私有并提供 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