Solved

wrapper class

Posted on 2004-10-04
5
175 Views
Last Modified: 2010-03-31
hi could some one please have a quick look over this. I think there is a prob with taking a value from the map. If anyone knows how to successfully fix this code would be most appreciated. It is trying to take a String, and go through each char, (char is key in map, number of occurences is the value) to count how many times a letter apears in the given String.

please help :)
thanks

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 = tmap.get(key);
           
            if (oldCount == null)
            {
                oldCount = new Integer(0);
                oldCount = new Integer(oldCount.intValue()+1);
                tmap.put(key,oldCount);
            }
        }
       
    }
   
}
0
Comment
Question by:ca_dyl
  • 3
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 12215792
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12215795
>>Integer oldCount = tmap.get(key);

should be

Integer oldCount = (Integer)tmap.get(key);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12215807

       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(0);
           }
           else {
             count = new Integer(oldCount.intValue() +1);
           }
           tmap.put(key, count);
       }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12215825
8-)

>>count = new Integer(0);

should be

count = new Integer(1);

sorry ;-)
0
 

Author Comment

by:ca_dyl
ID: 12215840
thanks for that, ive got another question following on from this... but il do another post so if u want some points :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

821 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