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.
if(!faculties[f-1].trim().equals(arr[0].trim()))returnstruewhenf == 1even ifarr[0] == "Math & CS"andfaculties[f-1] == "Math & CS"faculties[f-1]andarr[0]? If that returns anything other than 0, there's a character issue, as @litelite saysMath & CS Physics