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 |
.