0

I'm having troubles with an if statement to do something if the current string in a TextView is "Heads."

This is my current code:

 if (myCoin = "Heads") {

                 ImageView img = (ImageView) findViewById(R.id.imageView);
                 img.setImageResource(R.mipmap.heads);

             }

"myCoin" is the name of a string that contains the words "Heads" and "Tails."

final String[] myCoin= {"Heads", "Tails"};

When a random number is generated, the TextView displays one of those words depending on the number. However, I am not able to run the code due to the squiggly red-line under (myCoin = "Heads") that reads:

Incompatible Types.

Required: Java.Lang.String[]

Found: Java.Lang.String

As you can see, my string DOES have the square brackets, so I'm wondering if I can even use myCoin in an if statement.

SOLVED

John Hascall
  • 9,176
  • 6
  • 48
  • 72
IIMON
  • 5
  • 6
  • 2
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Mitch Wheat Jan 07 '16 at 01:06
  • myCoin is a `final` variable, testing if myCoin[0] is equals to `Heads` will always be true - what are you wanting to do? – Scary Wombat Jan 07 '16 at 01:09
  • myCoin is an array... and you are trying to see if that array, which you already defined as an array which has heads and tails, is equal to heads? – SlumpA Jan 07 '16 at 01:09
  • You can't compare string and string array. The string is contained in a string array. – The_Martian Jan 07 '16 at 01:20

5 Answers5

1

problem a)

= is an assignment, you are wanting .equals

problem b)

myCoin is an array

if you do

myCoin[0].equals ("Heads") 

it will always be true

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

TRY IT Best For Me if(Arrays.asList(myCoin).contains("Heads"))

Jignesh Mavani
  • 269
  • 1
  • 7
0
final String[] myCoin= {"Heads", "Tails"};

if (myCoin = "Heads") {
    ImageView img = (ImageView) findViewById(R.id.imageView);
    img.setImageResource(R.mipmap.heads);
 }

//----------------------------------------------------------

your variable is array. therefore your code look like this... ff:

final String[] myCoin= {"Heads", "Tails"};

int randomNumber = Math.random(10)%2;

if (myCoin[randomNumber].equals("Heads")){ //when you are in string condition symbol must .equals instead '=' .
   ImageView img = (ImageView) findViewById(R.id.imageView);
   img.setImageResource(R.mipmap.heads);
 }
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

myCoin is an array, so it could not be equal to a String.

You should use

if myCoin[random].equals("Heads") {
    // do something
}
Dennis Lu
  • 762
  • 2
  • 9
  • 21
0

If you want to compare two objects you have to use "==".

== is the equivalent of "Equals to" and = is the equivalent of "Set equal to"

//This checks if A is equal to one
if (A == 1){  
     //This sets A equal to one
     A = 1;
}

Also, you are trying to check the entire array when you put

  if (myCoin == "Heads") {
  }

You should be checking a String within the array by passing a number into it and then checking if the strings are equal. It is worth noting that when comparing strings you want to use the .equals() method. The code below will check to see if the first item in the array "myCoin" is equivalent to "Heads".

 if (myCoin[0].equals("Heads")){
 }

There is a very indepth explanation between the differences of == and .equals() method here.

Community
  • 1
  • 1