import java.io.*;
import java.util.*;
public class WordCount{
public static void main(String args[]){
/* File f=new File("article.txt");
System.out.println(f.getName());
System.out.println(f.getPath());
System.out.println(f.length());
System.out.println(f.exists());
Sytem.out.println(f.Parent());*/
try {
FileReader f = new FileReader("article.txt");
BufferedReader br = new BufferedReader(f);
String a;
StringBuffer b = new StringBuffer();
while ((a = br.readLine()) != null) {
b.append(a + "\n");
}
System.out.println(b);
f.close();
br.close();
String c=b.toString().toUpperCase();
System.out.println(c);
String str = "HELLO";
int i=0,count=1;
//for test
//System.out.println(c.indexOf(str,1));
while((c.indexOf(str,i))!=-1);
{
if(i>0)
{
if(c.indexOf(str,i)>c.indexOf(str,i-1))
count++;
}
i++;
}
System.out.println("count:"+count);
}
catch(IOException e){
e.printStackTrace();
}
}
}
代码如上,article里是这样的:
Hello hello HELLO Hello hello HELLO
Hello hello HELLO Hello hello HELLO
Hello hello HELLO Hello hello HELLO
Hello hello HELLO Hello hello HELLO
哦对了,题目的要求是不区分大小写,所以才在程序里加了一个uppercase。。
结果如下:
程序能编译通过啊,但是最后这步循环好像出了问题,有大佬能够解答吗?
发现了发现了,原来while那里多了个;笑哭