0

是的,这是“构建 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 对象首先将一行读入字符串,然后对字符串进行标记。我没有得到的是我如何将标记读入字符串,然后立即将其与下一个标记进行比较。

CONSTRAINT -> 这是来自数组之前的一章,所以我想不使用一个来解决。

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

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


        }
    }
}
4

3 回答 3

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);
        }
    }
}
于 2012-12-06T06:12:18.500 回答
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!
        }
    }
}
于 2012-12-06T06:10:11.647 回答
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();

    }

}
于 2013-08-22T22:03:47.830 回答