Java 不能使用“整型”将整型转换为布尔型;indexOf();
我试图计算“ing”一词在用户询问的字符串中出现的次数,但我有一个错误,说它无法转换 我尝试使用s.indexOf(“ing”) 我希望输出会告诉我并计算它发生了多少次,但我有一个错误Java 不能使用“整型”将整型转换为布尔型;indexOf();,java,Java,我试图计算“ing”一词在用户询问的字符串中出现的次数,但我有一个错误,说它无法转换 我尝试使用s.indexOf(“ing”) 我希望输出会告诉我并计算它发生了多少次,但我有一个错误 使用s=in.nextLine()而不是next()来阅读整行内容 您需要进行计数,直到lookFor部分仍在word中,每次都用其他内容替换第一次出现的内容,并继续使用while循环 String lookFor = "ing"; while (s.indexOf(lookFor) != -1) { c
s=in.nextLine()
而不是next()
来阅读整行内容lookFor
部分仍在word中,每次都用其他内容替换第一次出现的内容,并继续使用while循环
String lookFor = "ing";
while (s.indexOf(lookFor) != -1) {
count++;
s = s.replaceFirst(lookFor, "ed");
}
同
String lookFor = "ing";
while (s.contains(lookFor)) {
count++;
s = s.replaceFirst(lookFor, "ed");
}
您应该对输入进行
循环
,直到字符串没有所需的可用文本
int count = 0;
while (s.indexOf("ing") != -1) {
s = s.replaceFirst("ing", "");
count++;
}
System.out.print("Total occurances : "+count);
我想你需要
if(s.indexOf(“ing”)!=-1{…}
或if(s.contains(“ing”){…}
返回一个int
,而不是布尔值。使用s.contains..@ShubhDixit s.contais或s.indexOf()并不是计算所有的单词,它只是给我第一次出现“ing”的时间。有什么想法吗?可能是
int count = 0;
while (s.indexOf("ing") != -1) {
s = s.replaceFirst("ing", "");
count++;
}
System.out.print("Total occurances : "+count);