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 Answer 1
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*
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