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?
hm.put(splitted[i], i);?