What String methods should I use for this?
I have loaded a file into a String, now I need to determine how many letter 'a' there are in the file and print the amount, etc. How can I accomplish this? I need a for loop to scan through but not sure how to implement it. Could it by like:
for(int i = 0; i < String.lenght(); i++){
here is the confusion
}
maybe add some if statements? Help. Thanks in advance.
Re: What String methods should I use for this?
You will need to compare each char in the String to 'a' using an in statement, if it is an 'a' increment a count.
Or you could use a regular expression.
Re: What String methods should I use for this?
Which of the String class methods look like they can be useful? You need to get at each character in the String to be able to test it. What methods return a char?
Re: What String methods should I use for this?
you can try this,this code should work:
[full solution deleted by KevinWorkman]
Re: What String methods should I use for this?
migongotar, don't post full code solutions. Spoon-feeding is NOT helping. Read this: http://www.javaprogrammingforums.com...n-feeding.html
Re: What String methods should I use for this?
You need to loop through the string, and each time round the loop, find the character at the position indicated by your loop counter, and if that character is an 'a', increment another counter.
Re: What String methods should I use for this?
Another alternative, if you wanted to count numbers of other characters too, is to use one of the java.util.Arrays.sort() methods on the character array alluded to in some of the other comments. Then you could produce counts of every character with a single pass through the array. If you had an array index type of for loop, you wouldn't need to increment a count, you could just remember the index the current character started at and take the difference of the indexes when the character next changes.