Java:使用 Scanner 对象在线计算重复标记

标签 java token java.util.scanner

是的,这是“构建 Java 程序”中的一个练习,但它不是分配的问题。

我需要编写一个读取以下文本作为输入的方法:

hello how how are you you you you  
I I I am Jack's Jack's smirking smirking smirking smirking smirking revenge  
bow wow wow yippee yippee yo yippee yippee yay yay yay  
one fish two fish red fish blue fish  
It's the Muppet Show, wakka wakka wakka  

并产生以下输出:

how*2 you*4
I*3 Jack's*2 smirking*4
wow*2 yippee*2 yippee*2 yay*3

wakka*3

现在我知道我必须使用 Scanner 对象首先将一行读入字符串中,然后对字符串进行标记。我不明白的是如何将一个标记读入字符串,然后立即将其与下一个标记进行比较。

约束 -> 这是来自数组之前的一章,所以我想在不使用数组的情况下解决。

这是我到目前为止的代码:

public class Exercises {

public static void main(String[] Args) throws FileNotFoundException {

  Scanner inputFile = new Scanner(new File("misc/duplicateLines.txt"));
  printDuplicates(inputFile);

}

public static void printDuplicates(Scanner input){

  while(input.hasNextLine()){

        //read each line of input into new String
        String lineOfWords = input.nextLine();
        //feed String into new scanner object to parse based on tokens
        Scanner newInput = new Scanner(lineOfWords);

        while(newInput.hasNext()){

            //read next token into String
            String firstWord = newInput.next();

            //some code to compare one token to another


        }
    }
}

最佳答案

不需要使用数组...你只需要在 while 循环中添加一点状态:

public class Exercises {

    public static void main(String[] Args) throws FileNotFoundException {

      // scanner splits on all whitespace characters by default, so it needs
      // to be configured with a different regex in order to preserve newlines
      Scanner inputFile = new Scanner(new File("misc/duplicateLines.txt"))
          .useDelimiter("[ \\t]");

      printDuplicates(inputFile);
    }

    public static void printDuplicates(Scanner input){

        int lastWordCount = 0;
        String lastWord = null;

        while(newInput.hasNext()){

            //read next token into String
            String nextWord = newInput.next();

            // reset counters on change and print out if count > 1
            if(!nextWord.equals(lastWord)) {
                if(lastWordCount > 1) {
                    System.out.println(lastWord + "*" + lastWordCount);
                }
                lastWordCount = 0;
            }

            lastWord = nextWord;
            lastWordCount++;
        }

        // print out last word if it was repeated
        if(lastWordCount > 1) {
            System.out.println(lastWord + "*" + lastWordCount);
        }
    }
}

关于Java:使用 Scanner 对象在线计算重复标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13737817/

相关文章:

Java:按输入字符串的标记读取标记

java - Dijkstra邻接表

ios - 带有 redirectURI 的 Swift OAuth2.0

java - Java 中用于身份验证的 TokenStorage

java - 我需要帮助确保用户输入是整数且大于 0

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 如何使用 ObjectWeb ASM 检查字节码操作 PUTFIELD 正在重新分配属于 'this' 对象的字段?

java - 正确的 Java RTP/RTCP 堆栈

java - 如何在 setoncompletionlistener 之后使用相同的按钮?

php - 使用解析器标记删除 PHP 中允许的空格