Solved

wrapper class

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 50
strCount chalenge 3 50
Error with Java/Cache JDBC Classpath 2 20
Java JRE greater than 1.6 5 23
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

19 Experts available now in Live!

Get 1:1 Help Now