Java:使用Scanner对象在线计算重复令牌

Java:使用Scanner对象在线计算重复令牌,java,token,java.util.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 fi

**是的,这是“构建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对象将一行读入字符串,然后使用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(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);

    String previousWord = "";
    String currentWord = "";
    while(newInput.hasNext()){

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

        if (currentWord.equals(previousWord)) {
            // duplicate detected!
        }
    }
}

无需使用数组…您只需在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);
        }
    }
}

不太管用。为每个副本创建一个单独的行并忽略一些:how*2 I*3 Jack的*2 Smiking*5 wow*2 yippee*2 yippee*2@PatK默认情况下,
扫描器
在任何空白处中断。您希望保留哪些空白?只有换行和回车?是的,需要换行。这就是为什么在我最初的尝试中,我首先将整行读入一个字符串,然后将该字符串标记为第二个Scanner对象。我很感谢你在stevevls上查看这个。输出肯定需要与OP.@PatK中描述的完全一样。您需要了解两种方法:,它们可以将扫描仪配置为按给定模式分割。因为我会疏忽地给你一个解决方案,将空格和制表符视为“除CR和LF之外的空白”。)我如何“记住”并比较“第一个字”和“第二个字”物联网创建上面发布的输出。仅检测重复,但不跟踪状态(例如,同一标记的3个出现在一行上)。我可能遗漏了什么。@PatK True,我没有试图解决整个难题,我只是回答了他明确要求的问题,即如何比较扫描仪按顺序返回的两个单词。这个问题的答案是:使用两个变量。:-)我希望他能把剩下的拼凑起来。
public class test2 {

  public static void main(String[] args) {

    Scanner input = null;
    try {
      input = new Scanner(new File("chinese.txt"));
    } catch (FileNotFoundException e) {

      e.printStackTrace();
    }

    String currentLine;
    String lastWord="";
    String currentWord="";
    int count=1;
    while (input.hasNextLine()){
       currentLine=input.nextLine();
       Scanner newInput = new Scanner (currentLine);
       //System.out.println(currentLine);
       while(newInput.hasNext()){

         currentWord=newInput.next();
         if (!currentWord.equals(lastWord)&& count>1){
           System.out.print(lastWord+"*"+count+" ");
           count=1;


         }
         else if (currentWord.equals(lastWord)){
           count++;

         }
         lastWord=currentWord;

       }
       if (count>1){
         System.out.print(lastWord+"*"+count+" ");


       }
       System.out.println();
       count=1;

    }

    input.close();

    }

}