The Artima Developer Community
Sponsored Link

Java Answers Forum
Count Character Occurrence

3 replies on 1 page. Most recent reply: Apr 17, 2003 3:02 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Kevin

Posts: 27
Nickname: quasi
Registered: Apr, 2003

Count Character Occurrence Posted: Apr 16, 2003 1:45 AM
Reply to this message Reply
Advertisement
How do I display the occurence of each character in a string so that no character is repeated when printed? eg
NAME: Johnny
CHARACTER COUNT
J: 1 0: 1 h:1 n:2 y:1
I know how to display every character and their occurences and my code is below. Any help would be appreciated
for (i=0; i<surname.length(); i++) {
count=0;
for(j=0;j<surname.length();j++) {
if(surname.charAt(i)==surname.charAt(j)) {
count++;}
}
System.out.println(surname.charAt(i) + " " + count);

}


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Count Character Occurrence Posted: Apr 16, 2003 7:21 AM
Reply to this message Reply
To display only each character in a string once...

public void displayEachCharOnce( String str )
{
    String noRepeat = new String();
    for( int i = 0; i < str.length(); i++ )
    {
        // get the next character in string str
        char c = str.charAt( i );
        // check to see if character is in noRepeat string
        if( noRepeat.indexOf( c ) == -1 )
        {
            // character is not in noRepeat 
            // so add it to the end of noRepeat string
            noRepeat = noRepeat.concat( String.valueOf( c ) );          
        }
    }
    // print out the string with no repeating characters
    System.out.println( noRepeat );
}


Is this what your looking for Kevin?

Adam

Kevin

Posts: 27
Nickname: quasi
Registered: Apr, 2003

Re: Count Character Occurrence Posted: Apr 17, 2003 1:34 AM
Reply to this message Reply
Thanks Adam for the answser
The code print each character once but I also want it to print how many times each character occurs in the String. Can you help

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Count Character Occurrence Posted: Apr 17, 2003 3:02 AM
Reply to this message Reply
Sure thing Kevin. How about this?

import java.util.Enumeration;
import java.util.Hashtable;
 
public class CountChar
{
  public void countEachChar( String str )
  {
    Hashtable hashtable = new Hashtable();
    for( int i = 0; i < str.length(); i++ )
    {
      // get the next character in the string
      Character c = new Character( str.charAt( i ) );
      // check to see if the character is in the hashtable
      if( hashtable.contains( c ) )
      {
        // get the related character occurrence count
        Integer i = (Integer) hashtable.get( c );
        // increment the character count
        i = new Integer( i.intValue() + 1 );
        // put the character count back into the hashtable
        hashtable.put( c, i );
      }
      else
      {
        // add the character to the hashtable
        hashtable.put( c, new Integer( 0 ) );
      }
    }
 
    // print out the characters and the no of occurrences
    Enumeration enum = hashtable.keys();
    while( enum.hasNext() )
    {
       // get the next key
       Character c = (Character) enum.next();
       // get the related value
       Integer i = (Integer) enum.get( c );
 
       // print to display
       System.out.println( c + " : " + i );
    }
  }
}


Adam

Flat View: This topic has 3 replies on 1 page
Topic: Forms: How to pass them and use them after they are passed. Previous Topic   Next Topic Topic: Java mail - unknown host

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use