【问题标题】:Reading .txt information and loading it into an array读取 .txt 信息并将其加载到数组中
【发布时间】:2015-02-10 17:06:21
【问题描述】:

所以,基本上,我会有一个名为“data.txt”的文件

此文件将包含

Name;Age;State

示例

Anne;19;S (Single/Married/etc)
Gustav;18;S
Dinisio;19;C

基本上,我想读取文件,将信息放入数组中,并显示/列出它,例如:

Name: Anne
Age: 19
State: S

Name: Gustav
Age: 18
State: S

Name: Dinisio
Age: 19
State: C

为此,我创建了一个具有以下结构的类:

    class Person {
    String name;
    int age;
    char state;
    }

变量:

    int i, option;
    String s;
    File f = new File("C:\\Users\\Gustavo\\Desktop\\data.txt");

    Person arrayPerson[] = new Person[4]; // It has 4 persons only

我有以下菜单:

    System.out.print("Choose an option: ");
    System.out.println("\n1- Load");
    System.out.println("2- List");
    System.out.print("Option: ");
    option= ler.nextInt();
    System.out.println("");

    switch(option)
    {
        case 1:
        {
            Exercicio07Java.openRead(f);
            while((s = Exercicio07Java.readLine()) != null)
            {
                String[] str = s.split(";");
                arrayPerson[i] = new Person();
                arrayPerson[i].name = str[0];
                arrayPerson[i].age = Integer.valueOf(str[1]);
                arrayPerson[i].state = str[2].charAt(0);
                i++;
            }
            Exercicio07Java.closeRead();
            break;
        }

        case 2:
        {
            for(i = 0; i < 4; i++)
            {
                System.out.println(arrayPerson[i].nome);
                System.out.println(arrayPerson[i].idade);
                System.out.println(arrayPerson[i].estado);
            }
            break;
        }
    }

我或多或少知道如何读取信息,但我不知道如何将它传递到数组中,而它被 ;

有什么想法吗?

【问题讨论】:

标签: java file


【解决方案1】:

使用 String.Split(String delimiter) 它返回一个 String[]。如果您将整行作为字符串读取并创建了一个新人。见下文。这可能是寻找您的好地方。

for(int i = 0; i < persons.size(); i++) {
     String [] info = /**Line From File Here As A String*/.split(";");
     persons[i].name = info[0];
     persons[i].age = info[1];
     persons[i].state = info[2];
}

【讨论】:

  • 您还必须将 info[1] 和 info[2] 转换为正确的数据类型。
  • 我已经将我的帖子编辑为我的实际代码,因为它已经有一段时间了,它不起作用吗?还是别的什么?
  • 我不确定到底要问什么。但是,我认为你所拥有的应该可以很好地发挥作用。
  • 我找到了我需要的东西,我应该添加 people[i] = new Person();里面
  • 啊是的,我的错是你的人[]没有用“虚拟”人初始化。对不起:(
【解决方案2】:

比如说,如果你有四行,你可以这样做:

int index = 0;
while//read line in a file {
    String[] str = lineFromFile.split(";");//will split line on semicolon and you have name in index 0 of array, age in index 1 of str array and so on
    //validate you have all three parameters that you need to create person object.
    Person person = new Person(str[0], convertToInt(str[1]), str[2].charAt(0));//assuming you have constructor that accepts name, converted method to convert age to integer, state
    arrayPerson[index++]=person;
}

【讨论】:

    【解决方案3】:

    您可以使用 bufferedreader 读取行并使用 ; 分割标记 在此之后 将数组属性设置为 arrayPerso[i].name = temp[0] //这里 temp 是字符串数组,用于存储拆分值

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2022-10-06
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多