为什么 word.compareToIgnoreCase(s) == 0 就可以有true,word == s.toLowerCase();就全为false

ztisdashen 2019-04-05 11:43:35
strss是包含to的一串字符句子;
public class THREAD_NOTE {
public static void main(String[] args) {
String word = "to";
String[] strs = {"E:\\java\\newjava\\图片\\t1.txt",
"E:\\java\\newjava\\图片\\t2.txt",
"E:\\java\\newjava\\图片\\t3.txt",
"E:\\java\\newjava\\图片\\t4.txt"};
Path[] ps = new Path[strs.length];
for(int i=0;i<strs.length;i++) {
ps[i] = Paths.get(strs[i]);
}
Collection<Path> p= Arrays.asList(ps);
Set<Path> paths = new HashSet<>(p);
for(Path path:paths) {

int count = 0,sum = 0;double countd = 0;
try (BufferedReader br = new BufferedReader(new FileReader(path.toString()));)
{
String str = br.readLine();
while(str != null) {
String[] strss = str.split("[ ,.:;\"]");

sum += strss.length;
for(String s:strss) {
if(word.compareToIgnoreCase(s) == 0)
count += 1;
}
str = br.readLine();
}
//countd = (double)((count/sum)*100);
System.out.println((count*1.0/sum));
}catch(Exception e) {e.printStackTrace();}

}
}
}
...全文
92 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
流泪熊猫头 2019-04-05
  • 打赏
  • 举报
回复
因为字符串里大小写字母(E为大写字母)都有,toLowerCase()全转为小写,两边就不等了。

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧