Solved

wrapper class

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

803 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