Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java string not equals

So I have String array

String[] faculties = new String[21];

and I need to fill it with the names of faculties from .txt file that looks like

Math & CS; First Speciality; First Program; etc

Math & CS; Second Speciality; First Program; etc

Math & CS; Second Speciality; Second Program; etc

Physics; First Speciality; First Program; etc

Physics; First Speciality; Second Program; etc

I use the next code to get these names

FileReader input = new FileReader("faculties1.txt");
BufferedReader bufRead = new BufferedReader(input);
String myLine;
int f = 0;

    while((myLine = bufRead.readLine())!= null){
        String[] arr = myLine.split(";");

        if(f == 0){
            faculties[f] = arr[0];
            f++;
        }
        else{
            if(!faculties[f-1].trim().equals(arr[0].trim())){
                faculties[f] = arr[0];
                f++;
            }
        }
    }

but when I try to check my array

for(int i = 0; i < f; i++){
        System.out.println(faculties[i]);
    }

console says me

Math & CS

Math & CS

Physics

I dont get why Java puts "Math & CS" second time to my array.

like image 375
slxvxBatih Avatar asked Jul 24 '26 10:07

slxvxBatih


1 Answers

I tried this it is worked fine and output returns as you expected I think, anyhow you need to throws exceptions.

Math & CS
Physics

According to me there can be,

mispelled character in your text file. Check using different text-editor rather than windows notepad.

UPDATE:

After added this answer I saw your last comment in question that your problem solved.

I am adding this comment because if someone did not see your comment this will be helpful to them.

I used UTF-8 .txt in my project to see cyrillic symbols in console, but when I changed .txt to ANSII console said me Math & CS Physics*

like image 58
Blasanka Avatar answered Jul 27 '26 01:07

Blasanka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!