如何使用扫描仪输入文件修复StringTokenizer上的错误计数?

时间:2019-04-10 01:45:29

标签: java.util.scanner filereader stringtokenizer

我正在编写代码以读取文本文件(其中只有1个句子),打印并计算其令牌。

在给StringTokenizer分配文本时遇到麻烦(第19行:stok = new StringTokenizer(sc.nextLine());)您能帮我找到一种更好的书写方式吗?

  import java.util.*;
import java.io.*;
public static void main(String[] args) throws Exception
        {
            File file = new File("C:\\Users\\nhock\\Desktop\\School Work\\UNCC Spring 2019\\1213\\word.txt");
            Scanner sc = new Scanner(file);
            StringTokenizer stok;
            while(sc.hasNextLine())
            {
            System.out.println(sc.nextLine());
            stok = new StringTokenizer(sc.nextLine());
        int count = stok.countTokens();
        System.out.println("Number of tokens: "+count);
        }

预期:我是蝙蝠侠

令牌数量:3

0 个答案:

没有答案