Dwight Fuentes
Dwight Fuentes

Reputation: 3

charAt cannot be resolved or is not a field. Did I missed some imports?

String word = JOptionPane.showInputDialog("Enter String"); 
for(int x = 0 ; x <= word.length() ; x++) {
     for( ch = 'a' ; ch <= 'z' ; ch++) {
         num++;
         if(word.charAt[x].equalsIgnoreCase(ch)) {
               int z += num;
               num = 0; 
         }

     }
}

Upvotes: 0

Views: 4955

Answers (3)

SpringLearner
SpringLearner

Reputation: 13844

String#charAt() is a method and hence should be written as charAt(x) not as charAt[x]

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691625

charAt is a method. And method arguments are between parentheses, not brackets:

word.charAt(x)

It returns a char, which is a primitive type. And primitive types don't have methods. So word.charAt(x).equalsIgnoreCase(ch) won't compile. If you want methods on Character, wrap the primitive type into a Character:

char c = word.charAt(x)
Character character = Character.valueOf(c);
...

Upvotes: 9

Emma Leis
Emma Leis

Reputation: 2900

charAt is a method, not an array. Where you have square brackets, they must be round brackets. E.g.:

if(word.charAt(x).equalsIgnoreCase(ch)) {

Upvotes: 0

Related Questions