Java的:使用.txt文件到每一行的具体指标分配到一个ArrayList(Java: Using

2019-10-29 05:55发布

我的.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

如果这个问题已经被问了,我道歉。 我无法找出如何通过围绕搜索做,但如果有一个参考图了这一点,我会关闭这个下来,去那里。 提前致谢!

Answer 1:

问题是你的逻辑。 要存储仅astring[0]items ArrayList和重写的值description各一次。 由于去年读取值结果存储在description你在循环打印。

我喜欢创建自定义类,如下所示。 (仅用于演示的目的,否则你将宣布你的领域和私人提供getter和setter)

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

现在,而不是创建字符串的ArrayList如下您将创建的MyObject的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


Answer 2:

你看到的是输出的原因是你没有 与名单中的代码一起保存的描述,这就是为什么过去的描述被保存在描述变量并非所有描述值范围内。

为了解决这个问题,你可以创建一个简单的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;
    }
}


文章来源: Java: Using .txt files to assign specific indexes of each line to an arraylist