SergioPD08
SergioPD08

Reputation: 43

Reading and printing a text file with delimiters in Java

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

Upvotes: 0

Views: 286

Answers (2)

Scott
Scott

Reputation: 5858

You forgot to set information=0 after each while iteration:

while((aux=br.readLine())!=null){
    information=0; // You forgot to update 'information' variable
    for(int i=0;i<aux.length(); i++){
    ...

Upvotes: 1

Kaan
Kaan

Reputation: 5794

This isn't an answer, but some general suggestions for the code you pasted.

Instead of doing:

FileInputStream fstream = new FileInputStream("usuarios.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

you can do this. You don't need to hold onto fstream or in, their only purpose is to set up a new BufferedReader. Also, anything that you open here, you should also close later.

BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("usuarios.txt"))));

Instead of throwing exceptions in your method signature like this:

public void Show() throws FileNotFoundException, IOException

remove them, and then put try...catch around your code, including something like e.printStackTrace() so that you can see information about what went wrong. In general, you should avoid throwing any exceptions to the caller unless the caller can be reasonably expected to know what to do. In this, your code could throw exceptions about a file that the caller may have no idea about – it just called Show(), then something blows up related to stream/reader problems with a file.

public void Show()

Wrap the whole thing in a try block so that you can add a finally clause to close the various input streams and reader, like this below. It's good practice to open things when you need them, and close them when you're done.

BufferedReader br = null;
try {
    try {
        br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("usuarios.txt"))));
        // your code goes here
    } finally {
        if (br != null) {
            br.close();
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Related Questions