I have been having a problem with the next step in the logic of my code. Basically I’m supposed to examine each line of a file looking for consecutive tokens on the same line, and print the duplicate token along with the number of times it consecutively occurs. Non repeated tokens aren’t printed.
here’s a sample file
/*
* sometext.txt
* hello how how are you you you you
I I I am Jack's Jack's 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 here’s some code that i’ve written.
package chapter6;
import java.util.*;
import java.io.*;
public class OutputDuplicates {
public static void main (String[] args) throws FileNotFoundException {
for (;;) {
Scanner scan = new Scanner(System.in);
prompt(scan);
}
}
public static void prompt(Scanner scan) throws FileNotFoundException {
System.out.println("What is the name of the file?");
String name = scan.next();
File inputFile = new File(name);
if (inputFile.exists()) {
Scanner read = new Scanner(inputFile);
while (read.hasNext()) {
String line = read.nextLine();
Scanner oneLine = new Scanner (line);
while (oneLine.hasNext()) {
String word = oneLine.next();
System.out.println(word);
}
}
} else if (!inputFile.exists()) {
prompt(scan);
}
}
}
Any insight to the logic from here out would be much appreciated.
Here you go buddy, it should work for you