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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:56:15+00:00 2026-05-13T11:56:15+00:00

I am using a MSDOS windows prompt to pipe in a file.. its a

  • 0

I am using a MSDOS windows prompt to pipe in a file.. its a regular file with words.(not like abc,def,ghi..etc)

I am trying to write a program that counts how many times each word pair appears in a text file. A word pair consists of two consecutive words (i.e. a word and the word that directly follows it). In the first sentence of this paragraph, the words “counts” and “how” are a word pair.

What i want the program to do is, take this input :

abc def abc ghi abc def ghi jkl abc xyz abc abc abc ---

Should produce this output:

abc:
abc, 2
def, 2
ghi, 1
xyz, 1

def:
abc, 1
ghi, 1

ghi:
abc, 1
kl, 1

jkl:
abc, 1

xyz:
abc, 1

My input is not going to be like that though. My input will be more like:
“seattle amazoncom is expected to report”
so would i even need to test for “abc”?

MY BIGGEST issue is adding it to the map… so i think

I think i need to use a map of a map? I am not sure how to do this?

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

I think the map would produce this output for me: which is axactly what i want..

Key    |    Value           number of times
--------------------------
abc    |    def, ghi, jkl    3  
def    |    jkl, mno         2

if that map is correct, in my situation how would i add to it from the file?
I have tried:

if(words.contain("abc"))        // would i even need to test for abc?????

{
 uniqueWords.put("abc", words, ?)  // not sure what to do about this?
}

this is what i have so far.

import java.util.Scanner;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.HashSet;

public class Project1
{
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in); 
    String word;
    String grab;
    int number;

    // ArrayList<String> a = new ArrayList<String>();
    // TreeSet<String> words = new TreeSet<String>();
     Map<String, Map<String, Integer>> uniquWords = new HashMap<String, Map<String, Integer>>();

    System.out.println("project 1\n");

    while (sc.hasNext()) 
    {
        word = sc.next();
        word = word.toLowerCase();

        if (word.matches("abc"))      // would i even need to test for abc?????
        {
            uniqueWords.put("abc", word);  // syntax incorrect i still need an int!
        }

        if (word.equals("---"))
        {
            break;
        }
    }

    System.out.println("size");
    System.out.println(uniqueWords.size());

    System.out.println("unique words");
    System.out.println(uniqueWords.size());

    System.out.println("\nbye...");
}
}

I hope someone can help me because i am banging my head and not learnign anything for weeks now.. Thank you…

  • 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-05-13T11:56:15+00:00Added an answer on May 13, 2026 at 11:56 am

    I came up with this solution. I think your idea with the Map may be more elegant, but run this an lets see if we can refine:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class Main {
    
       private static List<String> inputWords = new ArrayList<String>();
       private static Map<String, List<String>> result = new HashMap<String, List<String>>();
    
        public static void main(String[] args) {
    
            collectInput();
            process();
            generateOutput();
        }
    
         /*
         * Modify this method to collect the input
         * however you require it
         */
        private static void collectInput(){
            // test code
            inputWords.add("abc");
            inputWords.add("def");
            inputWords.add("abc");
            inputWords.add("ghi");
            inputWords.add("abc");
            inputWords.add("def");
            inputWords.add("abc");
        }
    
        private static void process(){
    
            // Iterate through every word in our input list
            for(int i = 0; i < inputWords.size() - 1; i++){
    
                // Create references to this word and next word:
                String thisWord = inputWords.get(i);
                String nextWord = inputWords.get(i+1);
    
                // If this word is not in the result Map yet,
                // then add it and create a new empy list for it.
                if(!result.containsKey(thisWord)){
                    result.put(thisWord, new ArrayList<String>());
                }
    
                // Add nextWord to the list of adjacent words to thisWord:
                result.get(thisWord).add(nextWord);
            }
       }
    
         /*
         * Rework this method to output results as you need them:
         */
        private static void generateOutput(){
            for(Entry e : result.entrySet()){
                System.out.println("Symbol: " + e.getKey());
    
                // Count the number of unique instances in the list:
                Map<String, Integer>count = new HashMap<String, Integer>();
                List<String>words = (List)e.getValue();
                for(String s : words){
                    if(!count.containsKey(s)){
                        count.put(s, 1);
                    }
                    else{
                        count.put(s, count.get(s) + 1);
                    }
                }
    
                // Print the occurances of following symbols:
                for(Entry f : count.entrySet()){
                    System.out.println("\t following symbol: " + f.getKey() + " : " + f.getValue());
                }
            }
            System.out.println();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a MSDOS to pipe in a file.. I am trying to
Using a msdos window I am piping in an amazon.txt file. I am trying
Is there something better than using MSDOS in a bat file to run commmand
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Using Rails 3.2.0 with haml and sass: I Would like to link an external
I'm using Code::Blocks with MinGW to write my C++ applications in Windows XP. Now
is there any way to write 16-bit MS-DOS programs using a Windows environment? I
I'm using Windows XP with the latest version of Cygwin. If I set the
I'm trying to hide a folder with C# using the MSDOS attrib command. For
Can somebody remember what was the command to create an empty file in MSDOS

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.