从数组中的文件存储日期

时间:2014-09-21 23:50:35

标签: java

我试图将数据文件作为参数读入并将其存储到数组中。我不知道如何去做。

在文件名中我有:

 Jane 24
 Cody 18
 Derek 25

我现在的样子如下:

public void readAndPrint(String filename) {
    try {
        Scanner scan = new Scanner(new File(filename));

            while (scan.hasNext()) {
            String person = scan.next();
            int age = scan.nextInt();
            String[] personString = person.split(", ");

            System.out.println("Person: " + person + ", Age: " + age);
            for (int i = 0; i < personString.length; i++){
                personString[i] = person;
                System.out.print(Arrays.toString(personString));
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

现在结果我看起来像这样:

[Jane]Person: Jane, Age: 24

[Cody]Person: Cody, Age: 18

[Derek]Person: Derek, Age: 25

我想要的结果:

Person: Jane, Age: 24

Person: Cody, Age: 18

Person: Derek, Age: 25

Jane, Cody, Derek, 

编辑!

0 个答案:

没有答案