Java string not equal
So I have an array of strings
String[] faculties = new String[21];
and I need to fill it with faculty names from a .txt file that looks like
Math and CS; First specialty; First program; etc.
Math and CS; Second specialty; First program; etc.
Math and CS; Second specialty; Second program; etc.
Physics; First specialty; First program; etc.
Physics; First specialty; Second program; etc.
I am using the following 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]);
}
the console tells me
Math and CS
Math and CS
Physics
I don't understand why Java is putting "Math and CS" a second time into my array.
source to share
I tried this, it works great and the output returns as you expected, I think you need to throw exceptions anyway.
Math & CS
Physics
For me maybe
invalid character in text file. Check without using notepad instead of a text editor.
UPDATE:
After adding this answer, I saw your last comment on the question stating that your problem has been resolved.
I am adding this comment because if someone has not seen your comment, it will be helpful for them.
I used UTF-8.txt in my project to see cyrillic characters in the console, but when I changed .txt to ANSII console told me Math and CS Physics *
source to share