繁体   English   中英

我如何使用 java 计算文本文件中的单词数

[英]how do i count words in a text file using java

我正在尝试计算文件中的总字数,但 java 给出了每行的总字数。 因为我正在使用.hasNextLine() 无论我如何尝试它都不会计算总行数。有没有办法告诉 java 计算每行总计并返回总行字的值

import java.io.File;//import file class
import java.io.IOException;//import file class to handle errors
import java.util.Scanner;
import java.io.FileNotFoundException;

public class readingfromafile {
public static void main(String []args) {
{
 try{
 File myobj = new File("C:\\Users\\cse21-037\\Documents\\assighnment java\\100 words\\brain drain.txt");
 Scanner myReader  = new Scanner(myobj);
  while (myReader.hasNextLine()) {
  String data = myReader.nextLine();
  System.out.println(data);
 
  
        int count = 1;
         for (int i = 0; i < data.length() - 1; i++)
        {
            if ((data.charAt(i) == ' ') && (data.charAt(i + 1) != ' '))
            {
                count++;
            }
        }
                
        System.out.println(count);
        
    }

  }
 
  catch (FileNotFoundException e) {
  System.out.println("error 404");
  e.printStackTrace();
  }
  }
  }
  }
  

使用全局变量来存储单词的总数。

import java.io.File;//import file class
import java.io.IOException;//import file class to handle errors
import java.util.Scanner;
import java.io.FileNotFoundException;

public class readingfromafile {
    public static void main(String[] args) {
        {
            try {
                File myobj = new File("C:\\Users\\cse21-037\\Documents\\assighnment java\\100 words\\brain drain.txt");
                Scanner myReader = new Scanner(myobj);
                int totalCount = 0;
                while (myReader.hasNextLine()) {
                    String data = myReader.nextLine();
                    System.out.println(data);

                    int count = 1;
                    for (int i = 0; i < data.length() - 1; i++) {
                        if ((data.charAt(i) == ' ') && (data.charAt(i + 1) != ' ')) {
                            count++;
                        }
                    }
                    totalCount = totalCount + count;
                    System.out.println(count);
                }
                System.out.println(totalCount);
            } catch (FileNotFoundException e) {
                System.out.println("error 404");
                e.printStackTrace();
            }
        }
    }
}

它应该工作。

我会这样做:

package stackoverflow;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileWordCounter {



    public static void main(final String[] args) throws IOException {
        final String fileName = args != null && args.length > 0 ? args[0] : "/test/file";
        final String text = Files.readString(Paths.get(fileName));
        final String[] parts = text.split("\\W+"); // use \\s+ to split whole words, use \\W+ to exclude non-word characters
        System.out.println("Got parts:\t" + parts.length);

        System.out.println("Counting words...");
        long counter = 0;
        for (final String part : parts) {
            if (part != null && part.trim().length() > 0) {
                System.out.println("\t[" + part + "]");
                ++counter;
            }
        }
        System.out.println("Counted words:\t" + counter);
        System.out.println("All done.");
    }



}

暂无
暂无

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

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