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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:36:55+00:00 2026-06-11T17:36:55+00:00

This should be quite simple (I think), but I just can’t get it right…:|

  • 0

This should be quite simple (I think), but I just can’t get it right…:|

The task is as follows:

Ask the user for some input. The input must be split in to single words and put into an array. All words should be counted. If equal words exists, they get a “+1” on the output.
Finally I want to print out and hopefully the right amount of counted words in a list. I got the first two columns right, but the word-counter of equal words gave me a headache. If a word is found to be equal, it mustnt appear twice in the generated list! :!

I am a complete JAVA newbie so please be kind on the code-judging. 😉

Here is my code so far:

package MyProjects;

import javax.swing.JOptionPane;

public class MyWordCount {
public static void main(String[] args) {

    //User input dialog
    String inPut = JOptionPane.showInputDialog("Write som text here");

    //Puts it into an array, and split it with " ".
    String[] wordList = inPut.split(" ");

    //Print to screen
    System.out.println("Place:\tWord:\tCount: ");

    //Check & init wordCount
    int wordCount = 0;

    for (int i = 0; i < wordList.length; i++) {

        for (int j = 0; j < wordList.length; j++){

            //some code here to compare
            //something.compareTo(wordList) ?

        }

        System.out.println(i + "\t" + wordList[i]+ "\t" + wordCount[?] );
    }

}
}
  • 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-11T17:36:56+00:00Added an answer on June 11, 2026 at 5:36 pm

    Thanks for trying to help me. -This is what I ended up doing:

    import java.util.ArrayList;
    
    import javax.swing.JOptionPane;
    
    public class MyWordCount {
    public static void main(String[] args) {
    
        // Text in
        String inText = JOptionPane.showInputDialog("Write some text here");
    
        // Puts it into an array, and splits
        String[] wordlist = inText.split(" ");
    
        // Text out (Header)
        System.out.println("Place:\tWord:\tNo. of Words: ");
    
        // declare Arraylist for words
        ArrayList<String> wordEncounter = new ArrayList<String>();
        ArrayList<Integer> numberEncounter = new ArrayList<Integer>();
    
        // Checks number of encounters of words
        for (int i = 0; i < wordlist.length; i++) {
            String word = wordlist[i];
    
            // Make everything lowercase just for ease...
            word = word.toLowerCase();
    
            if (wordEncounter.contains(word)) {
                // Checks word encounter - return index of word
                int position = wordEncounter.indexOf(word);
                Integer number = numberEncounter.get(position);
                int number_int = number.intValue();
                number_int++;
                number = new Integer(number_int);
                numberEncounter.set(position, number);
    
                // Number of encounters - add 1;
            } else {
                wordEncounter.add(word);
                numberEncounter.add(new Integer(1));
            }
    
        }
    
        // Text out (the list of words)
        for (int i = 0; i < wordEncounter.size(); i++) {
            System.out.println(i + "\t" + wordEncounter.get(i) + "\t"
                    + numberEncounter.get(i));
        }
    
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This task should be quite simple, but nothing I tried has worked. I'm just
It should quite simple algorithm, but I just can't get around it. I have
This should be quite simple, but I can't work out the syntax. I have
This seems like it should be easy, but I can't quite find an explanation
This should be simple, but I'm getting confused. I have a parent/child tables -
This should be quite a simple problem, I've come across it enough that there
This should be quite easy, and I have done some research on this. I
This should reduce the executable size quite a bit in some of my very
This basic app which was given to me as an project should run quite
This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]

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.