Jon Takagi
Jon Takagi

Reputation: 55

charAt() Not Working

So, neither of these parameters are working. I try to compile it, and I get the same error message for both:

unexpected type
required: variable
found   : value

if(word.charAt(val) = guess)
              ^

The carat is pointing to the parentheses, however, so I'm not sure its an error with the parameter. I looked at about 15 sites, all of them saying that it should be an integer between 0 and 1 less than the length of word

int val = 0;
if(word.indexOf(guess) > 0 )
{
    if(word.charAt(val) = guess)
    {
        screentxt[val] = guess;
    }
    if(word.charAt(0) = guess)
    {
        screentxt[0] = guess;
    }   
}

Upvotes: 1

Views: 4931

Answers (2)

Reimeus
Reimeus

Reputation: 159784

You are currently using assignments in your if expressions.

Use the == operator in your if statements to evaluate the char content:

if (word.charAt(val) == guess) 

Upvotes: 6

sampson-chen
sampson-chen

Reputation: 47267

This is actually attempting to do an assignment instead of a comparison:

if(word.charAt(val) = guess)

Change to

if(word.charAt(val) == guess)

In addition, you probably want to change:

if(word.indexOf(guess) > 0 )

to

if(word.indexOf(guess) >= 0 )

in the event that the guess char is actually the first char in word, in which case you can remove this since it's redundant:

if(word.charAt(0) == guess)
{
    screentxt[0] = guess;
} 

Upvotes: 2

Related Questions