Yes this is an exercise from “Building Java Programs”, but its not an assigned problem.
I need to write a method that reads the following text as input:
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
And produces the following as output:
how*2 you*4 I*3 Jack's*2 smirking*4 wow*2 yippee*2 yippee*2 yay*3 wakka*3
Now I know I have to use Scanner objects to first read a line into a String, the to tokenize the string. What I don’t get is how I read a token into a string, then immediately compare it to the next token.
CONSTRAINT -> This is from the chapter before arrays so I’d like to solve without using one.
Here is the code I have so far:
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 } } }
Advertisement
Answer
No need to use arrays…you just need a little bit of state in the while loop:
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); } } }