Java 字符串不等于
Java string not equals
所以我有字符串数组
String[] faculties = new String[21];
我需要用 .txt 文件中的院系名称填充它,看起来像
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
我使用下一个代码来获取这些名称
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++;
}
}
}
但是当我尝试检查我的数组时
for(int i = 0; i < f; i++){
System.out.println(faculties[i]);
}
控制台说我
Math & CS
Math & CS
Physics
我不明白为什么 Java 将“数学与计算机科学”第二次放入我的数组中。
我试过了,它运行良好,输出 returns 正如你所料,无论如何你需要抛出异常。
Math & CS
Physics
据我所知可以,
文本文件中的字符拼写错误。检查使用不同的文本编辑器而不是 windows 记事本。
更新:
添加此答案后,我看到你的最后一条评论,你的问题已解决。
我添加此评论是因为如果有人没有看到您的评论,这将对他们有所帮助。
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*
所以我有字符串数组
String[] faculties = new String[21];
我需要用 .txt 文件中的院系名称填充它,看起来像
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
我使用下一个代码来获取这些名称
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++;
}
}
}
但是当我尝试检查我的数组时
for(int i = 0; i < f; i++){
System.out.println(faculties[i]);
}
控制台说我
Math & CS
Math & CS
Physics
我不明白为什么 Java 将“数学与计算机科学”第二次放入我的数组中。
我试过了,它运行良好,输出 returns 正如你所料,无论如何你需要抛出异常。
Math & CS
Physics
据我所知可以,
文本文件中的字符拼写错误。检查使用不同的文本编辑器而不是 windows 记事本。
更新:
添加此答案后,我看到你的最后一条评论,你的问题已解决。
我添加此评论是因为如果有人没有看到您的评论,这将对他们有所帮助。
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*