我为了找到一个程序中的关键字例如"public"等有多少个,写了下面这段代码:
int countItems=1;
try
{
BufferedReader inputS = new BufferedReader(new FileReader(fileName));
String line = "";
line = inputS.readLine();
StringTokenizer secondWordFinder =
new StringTokenizer(line," \t\n");
while(secondWordFinder.hasMoreTokens())
{
if(secondWordFinder.nextToken().equals("public")||
secondWordFinder.nextToken().equals("private"))
countItems++;
}
inputS.close();
} catch(FileNotFoundException e)
{
System.out.println("File "+ fileName +" was not found");
}
catch(IOException e)
{
System.out.println("Erroe reading form file " +fileName );
}编译通过,也可以执行,但是计数器countItem的值始终是1,望指教
int countItems=1;
try
{
BufferedReader inputS = new BufferedReader(new FileReader(fileName));
String line = "";
line = inputS.readLine();
StringTokenizer secondWordFinder =
new StringTokenizer(line," \t\n");
while(secondWordFinder.hasMoreTokens())
{
if(secondWordFinder.nextToken().equals("public")||
secondWordFinder.nextToken().equals("private"))
countItems++;
}
inputS.close();
} catch(FileNotFoundException e)
{
System.out.println("File "+ fileName +" was not found");
}
catch(IOException e)
{
System.out.println("Erroe reading form file " +fileName );
}编译通过,也可以执行,但是计数器countItem的值始终是1,望指教
解决方案 »
- 写代码不知道从哪里下手?
- java swing窗口下的绘图
- 如何将获取的数据信息,插入到JdbTable相应的列当中?
- File 类问题!!
- File 高人请进 --> 如何将新建文件存放到指定的新建目录下, 文件和目录已创建, 就是不知道该怎么放
- 现在面临就业问题,请有Java工作经验的朋友帮忙出个主意!!!
- Java里面有没有win32里面的类似于getSystemMetrics的API?
- java写的客户端服务器简单通信问题
- 大家看看下面的代码有什么错误?
- 关于swing线程的问题
- new ImageIcon("yellow-ball.gif"), Color.YELLOW);不显示图片??
- 用浏览器和appletviewer命令查看Applet的区别
try
{
BufferedReader inputS = new BufferedReader(new FileReader(fileName));
String line = "";
while(line = inputS.readLine()!=null){
StringTokenizer secondWordFinder =
new StringTokenizer(line," \t\n");
while(secondWordFinder.hasMoreTokens())
{
if(secondWordFinder.nextToken().equals("public")||
secondWordFinder.nextToken().equals("private"))
countItems++;
}
}
inputS.close();
} catch(FileNotFoundException e)
{
System.out.println("File "+ fileName +" was not found");
}
catch(IOException e)
{
System.out.println("Erroe reading form file " +fileName );
}
我用你的程序编译了,但是编译报错:incompatible types
改为
while((line = inputS.readLine())!=null){
import java.util.StringTokenizer;
import java.io.*;public class Test {
private static int i = 0; public Test() {
i++;
} public static int getNum() {
return i;
} public static void main(String[] args) {
int countItems = 0;
String fileName = "FileTest.java";
try {
BufferedReader inputS = new BufferedReader(new FileReader(fileName));
String line = "";
while ((line = inputS.readLine()) != null) {
StringTokenizer secondWordFinder = new StringTokenizer(line,
" \t\n");
while (secondWordFinder.hasMoreTokens()) {
String str=secondWordFinder.nextToken();
if (str.equals("public")
|| str.equals("private"))
countItems++;
}
}
System.out.println("结果:" + countItems);
inputS.close();
} catch (FileNotFoundException e) {
System.out.println("File " + fileName + " was not found"+":"+e.getMessage());
} catch (IOException e) {
System.out.println("Erroe reading form file " + fileName+":"+e.getMessage());
} }}
补上,呵呵