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 9163355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:25:38+00:00 2026-06-17T14:25:38+00:00

I have been having a problem with the next step in the logic of

  • 0

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.

  • 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-17T14:25:40+00:00Added an answer on June 17, 2026 at 2:25 pm

    Here you go buddy, it should work for you

    public Map<String, Long> scan(File file) throws Exception {
    
            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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having a small problem with my code. I have been trying to read
I have been having this annoying problem when trying to implement a picture gallery
I have been having the following problem, i think it's probably due to the
Good day everyone. I have been having the same problem all day at work
EDIT I have finally figured out the problem i have been having on my
I am having a problem and have been banging my head aginst a wall...
I've been having a really weird problem. I have a mutable array that is
I am having a problem, for a while I have been trying to figure
I have been having this problem for a couple of weeks now. The problem
I have been having problems with creating a download list of files for a

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.