Question:
Help with a java assignment?
oskevinco
2008-12-31 19:42:40 UTC
I having a problem with this particular method in my program. It keeps throwing the array out of bounds error and I can't figure out why. Can anyone fix this? Thank you.

Calculate the count of the each letter in str,
Check for letter, non-letter
*/
public void calCount(String str)
{
char ch = ' ';
int i = 0;
for(i = 0; i < str.length(); i++)
{
ch = str.toUpperCase().charAt(i);

if(Character.isLetter(ch))
{
count[i - 'A']++; // 'A' for 0, 'B' for 1, ... 'Z' for 25
}
else
{
count[count.length - 1]++;
}
}

}
Three answers:
anonymous
2008-12-31 19:49:52 UTC
The ASCII value for "A" is not 0, so when you do:

count[i - 'A']++; // 'A' for 0, 'B' for 1, ... 'Z' for 25

it's going WAY out of bounds.

Also, this isn't really what you want to be doing anyway, you want to compare it to 'ch', not 'i'
angelia
2017-01-06 23:49:47 UTC
NetBeans has a tut on connecting Derby. that's undemanding. Derby comes with JavaSE. in the event that they have you ever use yet another DB besides Derby, seek NetBeans and MySQL and it will clarify the thank you to discover the JDBC driving force and use it. Later in life in case you stick to this, look into connecting Java with Hibernate working JPA. that's wonderful. JPA is the Java Persistance API and could be an ingredient to Java7. solid success.
anonymous
2008-12-31 20:32:14 UTC
If count is the array and holds only 26 then if A=65, you are way off.

If your string is apple, are you trying to use the ascii number of the letter as the index to put 1 in there, so if you get 2 letter P's you would have a 2 in there?

Then if so, it should be

count['A']++

I dont know why you would substract the size of count



I dont know what exactly count is. I'm just guessing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...