Scanning for duplicate lines in a file

I am having a problem with the next step in the logic of my code. Basically I have to check each line of a file looking for sequential tokens on the same line and print the duplicate token along with the number of times it happens sequentially. Non-repeating markers are not printed. here is an example file

/*
 * sometext.txt
 * hello how how are you you you you
I I I am Jack Jack 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 the Muppet Show, wakka wakka wakka
 */

      

and here is the code I wrote.

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 into the logic from here would be much appreciated.

+3


source to share


3 answers


Here you go, buddy, it should work for you

public java.util.Map scan (File file) throws Exception {



    java.util.Map<String, Long> map = new HashMap<>();
    Scanner read = new Scanner(file);
    while (read.hasNext()) {
        String line = read.nextLine();
        if(map.containsKey(line)) {
            map.put(line, map.get(line).longValue() + 1);
        } else {
            map.put(line, 1L);
        }
    }

    return map;

      

}

0


source


pseudocode:



for each line in the file
{
 lastword = ""
 numtimes = 1
 for each word in the line
 {
  if word == lastword
  {
   numtimes++
  }
  else
  {
   if numtimes > 1
   {
    print (/*print lastword and numtimes here*/)
   }
   lastword = word
   numtimes = 1
  }
 }
}

      

+1


source


You want to create a table of character frequencies:

Map<String, Integer> symbolFrequencies = new HashMap<String, int>();

      

Then, for each character, do the following:

Integer countForSymbol = symbolFrequencies.get(symbol);
if (countForSymbol==null){
    symbolFrequencies.put(symbol, 1);
} else {
    countForSymbol = new Integer(countForSymbol.intValue + 1);
}

      

and what he is. You will now have a score for all the symbols that you analyzed.

0


source







All Articles