1

So, basically, I will have a file called "data.txt"

This file will contain:

Name;Age;State

Example:

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

Basically, I want to read the file, put the information into an array, and show/list it, example:

Name: Anne
Age: 19
State: S

Name: Gustav
Age: 18
State: S

Name: Dinisio
Age: 19
State: C

For that I made a class with the following structure:

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

Variables:

    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

I have the following menu:

    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;
        }
    }

I know more or less how to read the information, yet I do not know how to pass it into an array, while it is being separated by ;

Any idea?

Ran
  • 153
  • 4
  • 16

3 Answers3

1

So say if you have four lines for example, you could do something like:

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;
}
SMA
  • 36,381
  • 8
  • 49
  • 73
1

Using String.Split(String delimiter) it returns a String[]. If you read in an entire line as a string and created a new person. See below. This might be a good place to look for you.

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];
}
IByrd
  • 177
  • 1
  • 13
0

you can use bufferedreader for read line and the split with ; marker and after this set the arrayproperty like arrayPerso[i].name = temp[0] //here temp is string array which is generated for split values storage

Ronak R
  • 79
  • 3