The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Chin up!

Posted by Matt Gerrans on November 11, 2001 at 3:35 AM

Don't be so hard on Chin, he is really just trying to help you by geting you to help yourself. It's "tough love."

Below is what I meant by filling out an array of ints. Using the map would be easy, too and you should give that a try for your own edification. It is exactly the same concept as I've done with the array, except the map keys could actually be the letters themselves (rather than an index) and the other item could be the string "other" or whatever you like.

Also, on your point of getting confused and forgetting what you know by looking at the book, I couldn't agree less! The mind is not limited to a fixed amount of knowledge, it expands as you learn. The more you know, the more you want to know and the more you realize how little you know. (That explains why teenagers often believe they are smarter and more knowledgible than their elders!)

- mfg


public class Charful
{
public static int[] getHistogram( String text )
{
text = text.toLowerCase();
int [] profile = new int[27];

for( int i = 0; i < text.length(); i++ )
{
char theChar = text.charAt(i);
if( 'a' <= theChar && theChar <= 'z' )
profile[theChar-'a']++;
else
profile[26]++;
}

return profile;
}

public static void main( String [] args )
{
String text = "'Twas brillig and the slithy toves " +
"did gyre and gimble in the wabe, " +
"all mimsy were the borogroves " +
"and the mome raths outgrabe...";

if( args.length > 0 )
text = args[0];

String alpha = "abcdefghijklmnopqrstuvwxyz?";
int[] phreak = getHistogram( text );
System.out.println( "The profile of \"" + text + "\"is: " );
for( int i = 0; i < phreak.length; i++ )
System.out.println( alpha.charAt(i) + "\t" + phreak[i] );
}
}


> I DIDN'T WANT TO BE PERSONAL....
> and For Matt..I sincerely apologize...
> For everyone else that I pissed off I also apologize...everyone's trying to help me but I may seem ungrateful not intentionally but I seem to be in a hurry to get
> answers perhaps thats why I cann't figure it out...

> I'll try to contemplate more and perhaps figure out the answer myself.
> Again I apologize to Matt....


>
> > i'm so sorry, perhaps i should explain these so that u're not confused.

> > > charAt() - to get the specific character from the string, and possibly the array, and compare both to see whether it's equal or not.

> > what i mean in "array" here is the "array" of your character list in the form of String. i.e. String charlist="abcde...". sorry to use the word "array" there. it doesn't literally mean the word array as in charlist[].

> > > indexOf() - well this is just to save you from looking at redundant characters, rather than checking every characters from the *beginning* of the array of either your string or character list (you can store your character list as a string, or well, an array), you just use indexOf() to get the first occurence of the character.

> > again, what i mean here is that, if you store your character list in the form of array (charlist[]) you would have to fetch every single character from the beginning itself to find out which letter was it. so, rather than all that hassle, an easier way is to store the character list as a String and retrieve the first occurence of the specific character (to know which character it is).
> >


> > String check="a";
> > String charlist="abcdef..."; (lazy to finish typing it :p)
> >
> > char ch=check.charAt(0); // retrieve the character
> > int loc=charlist.indexOf((int)ch);

> > // after u get the loc (which is of value zero, 0,
> > // which of course, signifies that it's an A) you can
> > // add to your counter which keeps track of how many times
> > // the specific character occurs.
> > // anyway, this is just a demonstration, you have to add your
> > // own for-loop to it. g'luck ;)
> >







Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us