简体   繁体   English

在Java中计算文件中的单词和字符数时出错

[英]Error in counting number of words and characters from a file in java

I have written the following code to print file content and print the number of character and words from file 我编写了以下代码来打印文件内容并打印文件中字符和单词的数量

import java.io.*;
import java.util.*;

 class Ass53
{ 


public static void main(String args[]) throws Exception
{
    File file=new File("sample.txt");
    Scanner sc=new Scanner(new FileInputStream(file));
    String line,line1;
    int count1=0;
    int count=0;        

    /*Loop for printing contents of file*/
    while(sc.hasNextLine())
    {
        line=sc.nextLine();
        System.out.println(line);
    }


    /*loop for counting number of character in file*/
    while(sc.hasNext())
    {
            line1=sc.next();
        for(int i=1;i<=line1.length();i++)
            count1++;
    }
    System.out.println(count1); 


    /*loop for counting number of words in a file*/
    while(sc.hasNext())
    {
            sc.next();
            count++;
    }
    System.out.println("Number of words: " + count);


    }


 }

The problem is that only first while loop is executing.I guess the reason may be the sc.nextLine for first while loop.After first while loop sc points to nothing i guess?. 问题是只有第一个while循环正在执行。我想原因可能是第一个while循环的sc.nextLine。在第一个while循环后sc指向什么都没有?

Is there any way to fix it? 有什么办法可以解决? I want that my other while loop also work 我希望我的其他while循环也能正常工作

Every time you are doing nextLine() you are advancing the scanner past the current line. 每次执行nextLine() ,都使扫描仪前进到当前行之外。 When the first loop is over, you are at the end of the file and there is nothing to scan. 当第一个循环结束时,您位于文件末尾,没有要扫描的内容。

A solution would be to recreate the scanner before each loop. 一种解决方案是在每个循环之前重新创建扫描仪。 Just repeat this before second and third while : 之前第二和第三只重复此while

sc=new Scanner(new FileInputStream(file));

Another solution would involve more work but is more elegant: use a single loop to store all lines in a List<String> , then analyse all lines to count words and characters. 另一种解决方案将涉及更多工作,但更优雅:使用单个循环将所有行存储在List<String> ,然后分析所有行以计算单词和字符。

You need to set your sc back to the beginning of the stream after you're done looping over it each time. 每次循环播放完后,都需要将sc设置回流的开头。

I won't post the code because this looks like homework, and I'm guessing the class name Ass53 is "Assignment53". 我不会发布代码,因为这看起来像是家庭作业,并且我猜测类名称Ass53为“ Assignment53”。 It shouldn't be too hard to look up and figure out how to set the stream back to the beginning. 查找并弄清楚如何将流重新设置为开始应该不难。

Of you could try to combine this into a single loop, because as it stands right now, you're actually reading the same file 3 times. 您可以尝试将其组合成一个循环,因为就目前而言,您实际上正在读取同一文件3次。 It might not be that bad if it's a small file, but if it's a larger file that would be slow. 如果它是一个小文件,可能不会那么糟,但是如果它是一个大文件,那会很慢。

The first loop say "until I run out of lines in the file, read the line and print it." 第一个循环说“直到我用完文件中的行,读取并打印它”。 Then the second loop looks for more characters in the file, and of course there's nothing there. 然后,第二个循环在文件中查找更多字符,当然那里什么也没有。

One option is to reset the scanner before each loop. 一种选择是在每个循环之前重置扫描仪。 Before each loop after the first, just close and discard the scanner, and create a new one. 在第一个循环之后的每个循环之前,只需关闭并丢弃扫描仪,然后创建一个新的扫描仪即可。

There's a better way, though. 不过,有更好的方法。 You can walk through the file character-by-character (eg with a BufferedReader ), and increment the character count on each character, and the newline count on each newline character. 您可以逐个字符浏览文件(例如,使用BufferedReader ),并增加每个字符的字符数,以及每个换行符的换行数。 Don't forget to account for files that don't end with a newline. 不要忘记考虑不以换行符结尾的文件。 Only one loop is required, and you've calculated both the total character count and the line count. 只需要一个循环,就可以计算出字符总数和行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM