I'm trying to read and print a text file with delimiters in Java. When I use the function it only prints the first object. I use Apache NetBeans 11.1. Here is my code:
public void Show() throws FileNotFoundException, IOException{
FileInputStream fstream=new FileInputStream("usuarios.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String aux;
String total = "";
String name="" , addres="";
String id = "";
int information=0;
try{
while((aux=br.readLine())!=null){
for(int i=0;i<aux.length(); i++){
if(aux.charAt(i)!='&'){
total+=aux.charAt(i);
}else{
information++;
switch(information){
case 1:{
id=total;
break;
}
case 2:{
name=total;
break;
}
case 3:{
addres=total;
break;
}
}
total="";
}
}
System.out.println("Id: " + id + " Name: " + name + " Address: " + addres);
}
}catch(IOException ex){
}
}
In text file I have:
1&John&Address #1&
2&Peter&Address #2&
3&Robert&Address #3&
My output:
Id: 1 Name: John Address: Address #1
Id: 1 Name: John Address: Address #1
Id: 1 Name: John Address: Address #1
Expected output:
Id: 1 Name: John Address: Address #1
Id: 2 Name: Peter Address: Address #2
Id: 3 Name: Robert Address: Address #3