SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
File Processing
Exercise:
Exercises
Question:12 | ISBN:9780136091813 | Edition: 2

Question

Write a method called printDuplicates that takes as a parameter a Scanner containing a series of lines. Your
method should examine each line looking for consecutive occurrences of the same token on the same line and print each duplicated token, along with the number of times that it appears consecutively. Nonrepeated tokens are notnprinted. You may ignore the case of repetition across multiple lines (such as if a line ends with a given token and the next line starts with the same token). You may assume that each line of the file contains at least 1 token of input. For example, consider the following input:
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

Your method should produce the following output:
how*2 you*4
I*3 Jack's*2 smirking*4
wow*2 yippee*2 yippee*2 yay*3
wakka*3

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package chap456;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class PrintDuplicates {

    public static void printDuplicates(Scanner input) {
        // keep reading from the input till end of the line
        while (input.hasNextLine()) {
            
            // set current string to null
            String currentString = null;
            Scanner line = new Scanner(input.nextLine());
            int count = 0;

            while (line.hasNext()) {
                String nextLine = line.next();

                // if the next line equals the current string increase the count 
                if (nextLine.equals(currentString)) {
                    count += 1;
                } else {
                    if (count > 1)
                        System.out.print(currentString + "*" + count + " ");
           
                   // set current string to next line
                    currentString = nextLine;
                    count = 1;
                }
            }

            if (count > 1)
                System.out.print(currentString + "*" + count);

            System.out.println();
        }
    }

   // driver method
    public static void main(String[] args) throws FileNotFoundException {
        String str = System.getProperty("user.dir") + "\\resources\\printDuplicates.txt";
        File file = new File(str);
        Scanner input = new Scanner(file);
        printDuplicates(input);
    }

}
input file:  printDuplicates.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
Output:

how*2 you*4
I*3 Jack's*2 smirking*4 
wow*2 yippee*2 yippee*2 yay*3

wakka*3

 

0 0

Discussions

Post the discussion to improve the above solution.