Welcome to Dream.In.Code
Become a Java Expert!

Join 150,116 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,964 people online right now. Registration is fast and FREE... Join Now!




HashMap

 
Reply to this topicStart new topic

HashMap, sorting

Need_Help_Badly
26 Jun, 2008 - 06:06 PM
Post #1

New D.I.C Head
*

Joined: 26 Jun, 2008
Posts: 1

I am trying to determine which number entered into a HashMap was entered the most. Then display the number and the occurrances.
this is what i have:
CODE

import java.util.*;

public class HashMap
{
    public static void main(String [] args) throws InputMismatchException
    {
        Scanner keyboard = new Scanner(System.in);  
        String storage = "", YN = "Y";
        int testInt, value = 0;
        boolean again = false;
        
        //Create a hash map to hold the numbers to count
        Map<String, Integer> hashMap = new HashMap<String, Integer>();
        
        do
        {
            try
            {
                System.out.print("Enter an integer: ");
                testInt = keyboard.nextInt();
            }
            catch(InputMismatchException ex)
            {
                System.out.print("Enter a number that is an integer.");
                testInt = keyboard.nextInt();
            }
            storage = storage + "." + testInt;
        }while(testInt != 0);
        
        String[]integers = storage.split("[.]");
        
        for(int i = 0; i < integers.length; i++)
        {
            if(integers[i].length() >= 1)
            {
                if(hashMap.get(integers[i]) != null)
                {
                    value = hashMap.get(integers[i]).intValue();
                    value++;
                    hashMap.put(integers[i], value);
                }
                else
                    hashMap.put(integers[i], 1);
            }
        }
        //create a tree map from the hash map
        Map < String, Integer > treeMap = new TreeMap < String, Integer > (hashMap);
        
        Arrays.sort(integers);
TreeMap(SortedMap hashMap);
        //display mappings
        System.out.println("display integers and thier count in ascending order");
        System.out.print(treeMap);
        System.out.println();
        
        System.exit(0);
    }
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: HashMap
26 Jun, 2008 - 09:23 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Not a good idea at all to named your class HashMap
When reading that code we never know (at first look) if you are refering Java standard class HashMap or your own class HashMap
Name it something else like NeedHelpBadlyHashMap and then I'll have a look at it
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:10AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month