0

I have a task from the data structure course that is getting complicated for me. The instruction is to read a text file, with the format: Ana,30,120|Raul,23,178|Laura,15,164; (with 200 elements), where the first value is the name, the second the age, and the third the height. I have to add to an ArrayList. I have the following code:

public void readFile()
{
    String lineas;
    try
    {
        InputStream fileInputStream = new FileInputStream("datos.txt");
        InputStreamReader reader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
        
        BufferedReader br = new BufferedReader(reader);
        while((lineas = br.readLine()) != null)
        {
            String[] valor = lineas.split(",");
            String name = valor[0];
            int age = Integer.parseInt(valor[1]);
            int height = Integer.parseInt(valor[2]);
            
            persona.add(new Persona(name, age, height));
            
            showMenuOptions();
        }
    } catch (FileNotFoundException ex) {
        System.out.println("File Not Found.");
    } catch (IOException ex) {
        System.out.println("Can't open the File.");
    }
}

But it only performs the search by separating each line at the end when it finds, however I need to modify so that it detects each value separated by , and separates them when finding the character | .

Navaneeth Sen
  • 6,315
  • 11
  • 53
  • 82
  • Does each line of the text file contain the name, age, and height of three people? – Gilbert Le Blanc Sep 17 '21 at 16:57
  • yes, the file contain for example: Ana,30,120|Raul,23,178|Laura,15,164..... (50 persons in a line, and at the end have a `;`) – Joseph Gonzalez Sep 17 '21 at 17:04
  • For each line, remove the semicolon at the end, split by |, split each piece by the comma. Create a new Persona and add him to the List. – Gilbert Le Blanc Sep 17 '21 at 17:09
  • What is the purpose of the semicolon? How does each line in the file look like? – Navaneeth Sen Sep 17 '21 at 17:17
  • 1
    The instructions of the homework is: Read text file that contains the following structure Name, Age and Height separated by commas and inside `|`, indicate as end of line a `;`, as maximum content it must contain 200 elements. The example of the file: `Ana,30,120|Raul,23,178|Laura,15,164;` – Joseph Gonzalez Sep 17 '21 at 17:27
  • Sounds to me like you have three delimiters: `;`,`|` and `,`. No line separators are mentioned, seemingly. Is that right? – g00se Sep 17 '21 at 22:26
  • Yes, the `|` for separate persons, the `,` for separate attributes and the `;` at the end of line with 50 persons. – Joseph Gonzalez Sep 17 '21 at 22:28
  • Right. Do you have a link to the actual input file? – g00se Sep 17 '21 at 22:29

2 Answers2

0

Refer the below link and you can use split() method.

Java reading a file into an ArrayList?

Manoj Piyumal
  • 219
  • 1
  • 2
  • 10
  • How about bro, if I already read the answers, however, I already read the file, I need to be able to separate by the character `|` and read a `;` at the end of the line. – Joseph Gonzalez Sep 17 '21 at 21:42
0
    public List<Person> loadPeopleFromFile(String path) {
        List<Person> result = new ArrayList<>();
        try {
            try (Scanner s = new Scanner(new File(path))) {
                s.useDelimiter("[|,;]");
                while (s.hasNext()) {
                    result.add(new Person(s.next(), s.nextInt(), s.nextInt()));
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();   
        }
        return result;
    }

should do it for you

g00se
  • 3,207
  • 2
  • 5
  • 9