Java:使用.txt文件将每行的特定索引分配给arraylist [英] Java: Using .txt files to assign specific indexes of each line to an arraylist

查看:112
本文介绍了Java:使用.txt文件将每行的特定索引分配给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

我正在尝试找出如何像代码一样更新描述.例如

I'm trying to figure out how to make the description update as the code's do. e.g.

code1 description1
code2 description2
code3 description3

如果已经问过这个问题,我深表歉意.我无法通过四处搜索来找到解决方法,但是如果有参考可以解决这个问题,那么我将关闭它并去那里.预先感谢!

If this question has been asked already, I apologize. I couldn't find out how to do it by searching around but if there's a reference to figure this out I'll close this down and go there. Thanks in advance!

推荐答案

问题出在您的逻辑上.您仅将astring[0]存储到items ArrayList中,并且每次都覆盖description的值.结果,读取的最后一个值存储在要在循环中打印的description中.

The problem is with your logic. You are storing only astring[0] to the items ArrayList and overwriting the value of description each time. As a result on last value read is stored in description which you are printing in the loop.

我更喜欢如下创建一个自定义类. (只是为了演示,否则您将声明字段为私有字段并提供获取器和设置器)

I prefer creating a custom class as follows. (Just for the sake of demo otherwise you would declare your fields as private and provide getters and setters)

class MyObject {
 public String code;
 public String description;
 public String price;
}

现在,您将创建MyObject的ArrayList,而不是创建String的ArrayList,

now instead of creating ArrayList of Strings you will create ArrayList of MyObject as follows

ArrayList<MyObject> items = new ArrayList<MyObject>();

现在,每当您读取一行时,就创建一个MyObject的新实例,如下所示用astring中的值填充其字段

now create a new instance of MyObject each time you read a line , populate its fields with the values from astring as follows

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循环将其打印出来

and then finally print it using the same foreach loop as follows

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

这篇关于Java:使用.txt文件将每行的特定索引分配给arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆