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

  • Home
  • SEARCH
  • 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 541721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:22:20+00:00 2026-05-13T10:22:20+00:00

Using a msdos window I am piping in an amazon.txt file. I am trying

  • 0

Using a msdos window I am piping in an amazon.txt file.
I am trying to use the collections framework. Keep in mind I want to keep this
as simple as possible.
What I want to do is count all the unique words in the file… with no duplicates.

This is what I have so far. Please be kind this is my first java project.

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

public class project1 {

    // ArrayList<String> a = new ArrayList<String>();

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

        int count = 0;
        ArrayList<String> a = new ArrayList<String>();
        // Iterator<String> it = a.iterator();

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

        while (sc.hasNext()) {      
            word = sc.next();  
            a.add(word); 
            if (word.equals("---")) {
            break;
            }
        }

        Iterator<String> it = a.iterator();

        while (it.hasNext()) {
            grab = it.next();

            if (grab.contains("a")) {
                System.out.println(it.next()); // Just a check to see
                count++;
            }
        }
        System.out.println("I counted abc = ");
        System.out.println(count);
        System.out.println("\nbye...");
    }
}
  • 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-13T10:22:20+00:00Added an answer on May 13, 2026 at 10:22 am

    In your version, the wordlist a will contain all words but duplicates aswell. You can either

    (a) check for every new word, if it is already included in the list (List#contains is the method you should call), or, the recommended solution

    (b) replace ArrayList<String> with TreeSet<String>. This will eliminate duplicates automatically and store the words in alphabetical order

    Edit

    If you want to count the unique words, then do the same as above and the desired result is the collections size. So if you entered the sequence “a a b c —“, the result would be 3, as there are three unique words (a, b and c).

    • 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 (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
I'm trying to hide a folder with C# using the MSDOS attrib command. For
I am using a MSDOS windows prompt to pipe in a file.. its a
Is there something better than using MSDOS in a bat file to run commmand
Using LINQ on collections, what is the difference between the following lines of code?
Using Nunit, I want to be able to write a test fixture that will
Using Flex 3, I would like to take an image snapshot such as this:
Using C# (the .NET framework), I'm looping through all the TimeZones and their AdjustmentRules...
I am using Visual Studio 2010 and I am trying to change the timen

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.