0

I am trying to create a program as a tutorial for myself for hashmaps. I ask the user into text and try to split it into hashmaps and then increase the count if the word repeats. This is my program:

import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
   public static void main(String args[])
   {
      {
      String s = JOptionPane.showInputDialog("Enter any text.");
      String[] splitted = s.split(" ");
      HashMap hm = new HashMap();
      int x;

      for (int i=0; i<splitted.length ; i++) {
         hm.put(splitted[i], i);
         System.out.println(splitted[i] + " " + i);
         if (hm.containsKey(splitted[i])) {
             x = ((Integer)hm.get(splitted[i])).intValue();
             hm.put(splitted[i], new Integer(x+1)); }
         }
      }
   }
}

When I input "random random random", I get: random 0 random 1 random 2

What do I need to change so I get: random 3 Also, do I need to use an iterator to print out the hashmap, or is what I used OK?

2
  • 1
    Why do you call hm.put(splitted[i], i);? Commented Dec 30, 2013 at 23:55
  • @MiserableVariable I found the syntax on the Java API and it was the variable that they were using in the example. I just forgot to change it. Commented Dec 31, 2013 at 1:14

3 Answers 3

5

Your initialization is wrong hm.put(splitted[i], i). You should initialize to 0 or to 1 (to count, not to index).

So do this loop first.

        for (int i = 0; i < splitted.length; i++) {
            if (!hm.containsKey(splitted[i])) {
                hm.put(splitted[i], 1);
            } else {
                hm.put(splitted[i], (Integer) hm.get(splitted[i]) + 1);
            }
        }

Then just do one more loop (iterate through the keys of the HashMap) and print the counts out.

        for (Object word : hm.keySet()){
            System.out.println(word + " " + (Integer) hm.get(word));
        }
Sign up to request clarification or add additional context in comments.

Comments

0
import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
   public static void main(String args[])
   {
      {
      String s = JOptionPane.showInputDialog("Enter any text.");
      String[] splitted = s.split(" ");
      Map<String, Integer> hm = new HashMap<String, Integer>();
      int x;

      for (int i=0; i<splitted.length ; i++) {
         if (hm.containsKey(splitter[i])) {
            int cont = hm.get(splitter[i]);
            hm.put(splitter[i], cont + 1)
         } else {
            hm.put(splitted[i], 1);
         }
      }
   }
}

Your Map declaration is wrong, remember the correct way to implement a Map.

3 Comments

His map declaration is not wrong, he just does not use generics (which is not recommended but is not wrong too).
@peter.petrov Oh okay, I've never seen that declaration before.
@Vannens I believe this came up with java 5 release.
0

This should work, its a pretty simple implementation..

        Map<String, Integer> hm = new HashMap<String, Integer>();
    int x;

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

        if (hm.containsKey(splitted[i])) {
            x = hm.get(splitted[i]);
            hm.put(splitted[i], x + 1);
        } else {
            hm.put(splitted[i], 1);
        }
    }

    for (String key : hm.keySet()) {

        System.out.println(key + " " + hm.get(key));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.