We had a lab last week that took too long and the teacher let us take it home to finish it. I got it all done and input all the values and stuff into Main using random stuff. The problem now is on a method that counts how many a's are in a string. The instructions say to find the number of times the character 'A' is found in a string...this is my code for it.
//method 6, returns how many times 'A' is found in the string
public static int countA (String s1)
{
int count = 0;
for (int index = 1; index <= s1.length(); index++)
{
if (s1.charAt(index)== ('a'))
count++;
}
return count;
}
I did System.out.println "(The number of a's in this string is: " +Myutilities.countA("My name is John Doe")); and I used my actual name in place of John Doe.
the error returned on the terminal window was "java.lang.StringIndexOutOfBoundsException: String index out of range: 27
at java.lang.String.charAt(String.java:687)
at MyUtilities.countA(MyUtilities.java:56)
at UtilitiesTest.main(UtilitiesTest.java:21)"
where it stopped the program.
any ideas on how to fix that method to make it work correctly? I guess some other people are having the same problem, because We all helped each other on this one.
I also got : "StringIndexOutOfBoundsException: String index out of range: 27 (in java.lang.String)" that popped up in the window with the methods on it.
Edited by: Taco_John on Oct 31, 2007 2:28 PM