Solved

wrapper class

Posted on 2004-10-04
5
173 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 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

20 Experts available now in Live!

Get 1:1 Help Now