Solved

wrapper class

Posted on 2004-10-04
5
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

622 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