Java 如何考虑新段落的第一个单词?
我正在尝试构建一个程序,它接收文件并输出文件中的字数。当所有内容都在一整段中时,它就完美地工作了。但是,当有多个段落时,它不会考虑新段落的第一个单词。例如,如果文件读取“我的名字是约翰”,程序将输出“4个单词”。但是,如果一个文件读取“My Name Is John”,并且每个单词都是一个新段落,则程序将输出“1个单词”。我知道这一定与我的if语句有关,但我假设在新段落之前有空格,可以考虑新段落中的第一个单词。 以下是我的一般代码:Java 如何考虑新段落的第一个单词?,java,string,character,Java,String,Character,我正在尝试构建一个程序,它接收文件并输出文件中的字数。当所有内容都在一整段中时,它就完美地工作了。但是,当有多个段落时,它不会考虑新段落的第一个单词。例如,如果文件读取“我的名字是约翰”,程序将输出“4个单词”。但是,如果一个文件读取“My Name Is John”,并且每个单词都是一个新段落,则程序将输出“1个单词”。我知道这一定与我的if语句有关,但我假设在新段落之前有空格,可以考虑新段落中的第一个单词。 以下是我的一般代码: import java.io.*; public class
import java.io.*;
public class HelloWorld
{
public static void main(String[]args)
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("health.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
int word2 =0;
int word3 =0;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
;
int wordLength = strLine.length();
System.out.println(strLine);
for(int i = 0 ; i < wordLength -1 ; i++)
{
Character a = strLine.charAt(i);
Character b= strLine.charAt(i + 1);
**if(a == ' ' && b != '.' &&b != '?' && b != '!' && b != ' ' )**
{
word2++;
//doesnt take into account 1st character of new paragraph
}
}
word3 = word2 + 1;
}
System.out.println("There are " + word3 + " "
+ "words in your file.");
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
import java.io.*;
公共类HelloWorld
{
公共静态void main(字符串[]args)
{
试一试{
//打开第一个文件
//命令行参数
FileInputStream fstream=新的FileInputStream(“health.txt”);
//使用DataInputStream读取二进制而不是文本。
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream));
弦斯特林;
int-word2=0;
int-word3=0;
//逐行读取文件
而((strLine=br.readLine())!=null){
//在控制台上打印内容
;
int-wordLength=strLine.length();
系统输出打印LN(斯特林);
for(int i=0;i
我已经尝试过在多个团队中调整if语句,但似乎没有什么不同。有人知道我把事情搞砸了吗
我是一个非常新的用户,几天前我问了一个类似的问题,人们指责我对用户要求太高,所以希望这能缩小我的问题范围。我真的很困惑,为什么不考虑新段落的第一个字。如果你需要更多的信息,请告诉我。谢谢 首先,您的计数逻辑不正确。考虑:
word3 = word2 + 1;
想想这是怎么回事。每次通过循环时,当您阅读一行时,您基本上都会对该行中的单词进行计数,然后将总计数重置为word2+1
。提示:如果要计算文件中的总数,则每次都要递增word3
,而不是用当前行的字数替换
其次,您的单词解析逻辑有点不正确。考虑一个空行的情况。您将看不到其中的任何单词,但您将行中的单词数视为word2+1
,这意味着您将空行错误地计算为1个单词。提示:如果行中的第一个字符是字母,则该行以单词开头
您的方法是合理的,尽管您的实现有点缺陷。作为另一种选择,您可能希望在每行中考虑<代码> String .Strut.()/<代码>。结果数组中的元素数是行中的字数
顺便说一下,如果为变量使用有意义的名称(例如,
totalWords
而不是word3
),则可以提高代码的可读性,并简化调试。如果段落不是以空格开头,则if条件不会计算第一个单词。
“我的名字是约翰”,程序将输出“4个单词”,这是不正确的,因为您错过了第一个单词,但在后面添加了一个。
试试这个:
String strLine;
strLine = strLine.trime();//remove leading and trailing whitespace
String[] words = strLine.split(" ");
int numOfWords = words.length;
我个人更喜欢这种带有基于令牌扫描的常规扫描仪。像这样的怎么样:
int words = 0;
Scanner lineScan = new Scanner(new File("fileName.txt"));
while (lineScan.hasNext()) {
Scanner tokenScan = new Scanner(lineScan.Next());
while (tokenScan.hasNext()) {
tokenScan.Next();
words++;
}
}
这将遍历文件中的每一行。对于文件中的每一行,它都会遍历每个标记(在本例中为单词)并增加单词数。我不确定“段落”是什么意思,但是我尝试按照您的建议使用大写字母,效果非常好。我使用了Appache Commons IO库
package Project1;
import java.io.*;
import org.apache.commons.io.*;
public class HelloWorld
{
private static String fileStr = "";
private static String[] tokens;
public static void main(String[]args)
{
try{
// Open the file that is the first
// command line parameter
try {
File f = new File("c:\\TestFile\\test.txt");
fileStr = FileUtils.readFileToString(f);
tokens = fileStr.split(" ");
System.out.println("Words in file : " + tokens.length);
}
catch(Exception ex){
System.out.println(ex);
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}哦,好吧,word3=word2+1背后的逻辑是,如果有人键入“我的名字是约翰”,则有3个空格,而4个单词。但是接下来的一点是,检查空格可能不是理想的方案,因此感谢您的提示。我试着给你一个提示,但是
+=
会为每一行增加word3
的值,而=
会有效地丢弃前面所有行的计数对不起,不知什么原因没读到。我试着加上这个