problem to compare two strings in java

Asked

Viewed 27 times

0

Hello, I read a text file File file = new File("/path/historical");

        String ler="";
        try{
            String urlF="/caminho/historico";
            FileReader fileReader = new FileReader(urlF);
            BufferedReader reader = new BufferedReader(fileReader);
            String data = null;
            while((data = reader.readLine()) != null){
                ler+=data;
            }
            fileReader.close();
            reader.close();
        }catch (FileNotFoundException e){
            ler="";
        }catch (IOException e){
            ler="";
        }

I need the saved information not to be repeated, so to record first analyze if you already have that information

String[] divLer=ler.split("#");

        ler="";

        for(int n=0;n<divLer.length;n++){
            if(divLer[n]!=sala){
                alerta(divLer[n]+" - "+sala);
                if(ler!="") ler+="#";
                ler+=divLer[n];
            }
        }
        if(ler!="") ler+="#"+sala;
        else ler+=sala;

Only that in this line if(divLer[n]!=room){ << the file room contains the same string as the object in the divLer[n] array, but it does not consider the two variables equal, and because of this repeats the information when saving the text file... someone knows what is wrong?

  • Considered comparing using the equals?

  • Switch to if(!divLer[n].equals(sala)){

  • 1

    Oh that’s right, I forget that difference in java... thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.