Solved

displaying treeMap values in alphabetical using comparator

Posted on 2004-10-04
15
749 Views
Last Modified: 2009-07-29
what i would like to be able to do is to iterate through the map, use a comparator to compare the keys to see which one comes first in the alphabet.. so that when they are printed they come out in alphabetical order... all good said in theory but i realize with a map u can not simply iterate through... just after some ideas if any have some!
cheers

import java.io.*;
import java.util.*;

public class charCount
{
   
   
    public static void main(String[] args)
    {
        String sentence = "hello world";
       
        TreeMap tmap = new TreeMap();
       
        for (int i=0; i<sentence.length(); i++)
        {
            Character key = new Character(sentence.charAt(i));
            Integer oldCount = (Integer)tmap.get(key);
            Integer count = null;

            if (oldCount == null)
            {
                count = new Integer(1);
            }
            else
            {
                count = new Integer(oldCount.intValue() +1);
            }
            tmap.put(key, count);
       }
       
       
       
    }
}
0
Comment
Question by:ca_dyl
  • 7
  • 3
  • 3
  • +2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12215859
Just use a TreeMap, then they will automatically
0
 

Author Comment

by:ca_dyl
ID: 12215877
cool so i dont have to use treeMap?? but then how do u print it to the console. for key & value...

sorry must be a total nuf nuf question but im only just starting :)

thx
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12215878
:-) You are anyway i see
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12215891
System.out.println(tmap);

Don't forget to *not* count spaces
0
 

Author Comment

by:ca_dyl
ID: 12215952
ok ok grasping all this slowly... last question i promise. I dont know how to get it so that the user can in put a string to be counted. Because at the mo i just used "hello world" so that i could check it workd, but how do i get user input?? i think i also fixed the space problemo.

thx

import java.io.*;
import java.util.*;

public class charCount
{
   
   
    public static void main(String[] args)
    {
        //charCount sentence = new charCount();
        //String sentence = new String();
        //String sentence = "hello world";
       
        TreeMap tmap = new TreeMap();
       
        for (int i=0; i<sentence.length(); i++)
        {
                if(i == ' ')
                {
                    return;
                }
                else
                {
                    Character key = new Character(sentence.charAt(i));
                    Integer oldCount = (Integer)tmap.get(key);
                    Integer count = null;
           
                    if (oldCount == null)
                    {
                        count = new Integer(1);
                    }
                    else
                    {
                        count = new Integer(oldCount.intValue() +1);
                    }  
                    tmap.put(key, count);
                }
   
       }
       
       System.out.println(tmap);
       
   
    }
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12215971
String sentence = args[0]; // should check to ensure there *is* an args[0]
0
 
LVL 9

Expert Comment

by:Venci75
ID: 12215972
you can iterate through a map:
for (Iterator it = tmap.entrySet().iterator(); it.hasNext(); ) {
  Map.Entry entry = (Map.Entry)it.next();
  System.out.print(entry.getKey());
  System.out.print(" - ");
  System.out.println(entry.getValue());
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:girionis
ID: 12215974
> Because at the mo i just used "hello world" so that i could check it
> workd, but how do i get user input?? i think i also fixed the space problemo.

If you mean user input from the keyboard have a look here: http://javaalmanac.com/egs/java.io/ReadFromStdIn.html

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12215975
btw, you'll have to pass the sentence in in quotes or you'll only get one word
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12215992
i.e.

java charCount "the quick brown fox"
0
 
LVL 92

Expert Comment

by:objects
ID: 12216106
using an array instead of a map makes it alot simpler:

import java.io.*;
import java.util.*;

public class charCount
{
    public static void main(String[] args)
    {
        if (args.length>0)
        {
         String sentence = args[0];
         int[] count = new int[128];  // you can adjust this according to what characters you are interested in
       
         for (int i=0; i<sentence.length(); i++)
         {
             count[sentence.charAt(i)]++;
         }
        }      
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12216122
you can then loop thru the count array to determine the occurrences:

for (int i=0; i<count.length; i++)
{
   if (count[i]>0 && i!=' ')
   {
      System.out.println(((char)i)+" occurred "+count[i]+" times");
   }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12216129
as well as being simpler code its also more efficient than using a map.
0
 

Author Comment

by:ca_dyl
ID: 12216137
cheers guys you have been so much help :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12216179
8-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now