Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:24:45+00:00 2026-06-15T16:24:45+00:00

Yes this is an exercise from "Building Java Programs", but its not an assigned

  • 0

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


        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:24:46+00:00Added an answer on June 15, 2026 at 4:24 pm

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes I know, this title isn't really helpfull but this is the exact problem.
Yes, this will be a homework (I am self-learning not for university) question but
I searched the site but it seems like this exercise from the book Learn
Yes this question has been asked before, but it didn't solve my problem. So
yes this is an iOS programming question. I need to calculate the exact time
(Before anyone says anything Yes this was homework but i have already turned it
I know the Sales pitch answer is yes to this question, but is it
Yes, I know this question has been asked before, but I can't find an
Yes, I know this question has been asked a lot of times, but I
I got this exercise, is not a homework , I just trying to solve:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.