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

时间:2012-12-06 05:59:12

标签: 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对象首先将一行读入String,以便对该字符串进行标记。我没有得到的是我如何将一个标记读入一个字符串,然后立即将它与下一个标记进行比较。

约束 - >这是从数组之前的章节开始的,所以我想在不使用数组的情况下解决。

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

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


        }
    }
}

3 个答案:

答案 0 :(得分:1)

不需要使用数组......你只需要在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);
        }
    }
}

答案 1 :(得分:0)

这个怎么样?我正在分配一个额外的字符串来跟踪前一个单词。

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!
        }
    }
}

答案 2 :(得分:0)

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();

    }

}