The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



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.



Posted by Chin Loong on November 10, 2001 at 10:55 PM

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 ;)


Sponsored Links

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