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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:47:24+00:00 2026-05-13T12:47:24+00:00

I am using a MSDOS to pipe in a file.. I am trying to

  • 0

I am using a MSDOS to pipe in a file.. 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

BTW: i am excluding “a”, “the”, “and” which has nothing to do with the word pair..

What is the best way to do this? please be nice, I am new to java..
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>();
        HashSet<String> uniqueWords = new HashSet<String>();

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

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

            if (word.matches("a") || word.matches("and") || word.matches("the"))
            {
            }
            else
            {
                uniqueWords.add(word);
            }

            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...");
    }
}

Sorry about the formatting. Its hard to get it right in here…

  • 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-13T12:47:24+00:00Added an answer on May 13, 2026 at 12:47 pm

    What about using a Map:

    Map<String, List<String>> words = new HashMap<String, List<String>>();
    

    The keys in the map would be unique words, and the values would each be lists of words that followed that unique word. The data structure might look like:

    Key    |    Value
    --------------------------
    abc    |    def, ghi, jkl
    def    |    jkl, mno
    
    • 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 windows prompt to pipe in a file.. its a
Using a msdos window I am piping in an amazon.txt file. I am trying
I am trying to write a batch script that combines several css files into
I need to write a simple terminal-based program that should, Read some text from
I'm trying to hide a folder with C# using the MSDOS attrib command. For
Is there something better than using MSDOS in a bat file to run commmand
i am trying to write a boot loader(hello world sort). i am using Bochs
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
I am using Visual Studio 2010 and I am trying to change the timen
Is there a command line tool that is able to search docx file under

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.