Java string not equals

Viewed 2111

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.

1 Answers
Related