简体   繁体   English

将文件生成的值列表转换为数组(Java)

[英]Convert list of values generated from file into arrays (Java)

I have some attributes of animals in a text file and I need to be able to extract the different Strings so I can put them into an Array of Arrays so that I can later add to them and edit the values etc. Here is the code I have already used to display them in a list but doesn't produce the arrays I want. 我在文本文件中具有动物的一些属性,我需要能够提取不同的字符串,以便将它们放入数组数组中,以便以后可以添加到它们中并编辑值等。这是我的代码已经习惯了将它们显示在列表中,但没有产生我想要的数组。

   public class FileReading {

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("Animals.txt"));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
            if(line != null) {
            System.out.println(line);
            }
        }
    } finally {
        br.close();
    }
}

It goes along the format of: Given name, what animal it is, the price, sex, colour, date brought (although on some lines there are extra attributes such as if something is poisonous , upto 7 strings on a line. 它遵循以下格式:给定名称,它是什么动物,价格,性别,颜色,带来的日期(尽管在某些行上还有一些额外的属性,例如某些东西有毒,一行上最多7个字符串。

so far my program displays the values of: 到目前为止,我的程序显示以下值:

  • John, Dog, 45.50, male, blue, 2013-11-02 约翰,狗,45.50,男性,蓝色,2013-11-02
  • Juno, Cat, 188, male, light brown, 2017-10-04, 2017-10-14 朱诺,猫,188,男,浅棕色,2017-10-04,2017-10-14
  • Ronaldo, Sppitting Cobra, 129.99, female, black, 2018-01-13 罗纳尔多,眼镜蛇,129.99,女,黑色,2018-01-13

and so on for many lines that are in the text. 对于文本中的许多行,依此类推。 Any help would be appreciated as I'm completely stuck as to how I would do this. 任何帮助将不胜感激,因为我完全不知道该怎么做。

I am a little confused. 我有点困惑。 Where are you declaring an array and attempting to add values to it? 您在哪里声明数组并尝试向其中添加值? Something like the following: 类似于以下内容:

try {
  FileReader fileReader = new FileReader(filename);
  BufferedReader bufferedReader = new BufferedReader(fileReader);
  List<String> lines = new ArrayList<String>();
  String line = null;
  while ((line = bufferedReader.readLine()) != null) {
     lines.add(line);
  }
  bufferedReader.close();    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM