关于字数统计的小问题,我的程序只能查奇数行

nickwuu 2004-04-07 07:39:28
[:)] 大家好,我在写一个统计字数和特定词的程序。 下面的是main method, 我还没有联接其他的class。 下面这个程序有点问题,只能统计奇数行,请大家帮忙看下,谢谢

另外还有一个问题,就是下面这个程序怎么与其他class最好的相连,包括一个数词class,一个数句子class,还有一个信息的vector。


import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

public class main
{
public static void main(String[] args) throws IOException
,FileNotFoundException
{
StringTokenizer tokenizer;
String fileAddress; //the address of the file
String addressInput; //the file name entered by the user
String s; //the token
int lineNo; //the number of lines in the text
int i;
int counter; //the number of all the words in the text
// TokenCounter tokenCounterA;


addressInput = JOptionPane.showInputDialog("Enter the file name:");


// while loop starts, prompt the user to input the file name, and the whole
// program starts.. when the program ends, prompt the user to enter a new file
// name or enter nothing to terminate the program
while(addressInput.length() > 0)
{
fileAddress = "h:\\" + addressInput;
BufferedReader inFile =
new BufferedReader(new FileReader(fileAddress));


tokenizer = new StringTokenizer(inFile.readLine());
s = tokenizer.nextToken();

lineNo = 1;
counter = 0;
while (s.length() > 0)
{
counter++;
System.out.println("Line" + lineNo + ": " + s );

if(tokenizer.hasMoreTokens())
s = tokenizer.nextToken();

else
{
if(inFile.readLine() != null)
{
tokenizer = new StringTokenizer(inFile.readLine());
lineNo++;
}
else
s = "";
}
}

System.out.println("All words:" + counter);

//prompt the user to enter another file name
addressInput = JOptionPane.showInputDialog("Enter the file name:");

}//while loop ends

//terminate the program
System.exit(0);
}


}

...全文
128 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
nickwuu 2004-04-07
  • 打赏
  • 举报
回复
解决了,多谢兄弟了 请收分

tokenizer = new StringTokenizer(inFile.readLine());


s = tokenizer.nextToken();

lineNo = 1;
counter = 0;
while (s.length() > 0)
{
counter++;
System.out.println("Line" + lineNo + ": " + s );

if(tokenizer.hasMoreTokens())
s = tokenizer.nextToken();

else
{
String line = inFile.readLine();
if(line != null)
{
tokenizer = new StringTokenizer(line);
lineNo++;
s = tokenizer.nextToken();
}
else
s = "";
}
}
nickwuu 2004-04-07
  • 打赏
  • 举报
回复
tokenizer = new StringTokenizer(inFile.readLine();


s = tokenizer.nextToken();

lineNo = 1;
counter = 0;
while (s.length() > 0)
{
counter++;
System.out.println("Line" + lineNo + ": " + s );

if(tokenizer.hasMoreTokens())
s = tokenizer.nextToken();

else
{
String line = inFile.readLine();
if(line != null)
{
tokenizer = new StringTokenizer(line);
lineNo++;
}
else
s = "";
}
}

多谢楼上的sagittarius1979兄弟,是这样改么? 但是运行每行最后一个词会出现两次 请问怎么解决? 我彻底晕了 谢谢 =)
sagittarius1979 2004-04-07
  • 打赏
  • 举报
回复
tokenizer = new StringTokenizer(inFile.readLine());
读了一行
if(inFile.readLine() != null)
{
又读了一行,
tokenizer = new StringTokenizer(inFile.readLine());
再读了一行,所以只是基数行。
.....
}

改成
String line = inFile.readLine();
if(line != null)
{
tokenizer = new StringTokenizer(line);
.....
}

就可以了。
HTML表格通过
等标签构建、列与表头结构,形成语义清晰的数据展示框架。在DOM中,每个表格元素均可被JavaScript访问与操作,实现动态内容更新。通过原生JS可获取表格节点并调用其方法,为后续增删改功能奠定基础。是原生 JavaScript 中用于创建任意 HTML 元素节点的核心方法。相较于和这类专用于表格的操作 API,提供了更强的灵活性和控制力,尤其适用于需要插入包含输入框、下拉菜单、按钮或其他复杂控件的表格场景。

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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