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?