-1

I have a String Array called copy. I want to check if copy[0] matches with copy[2] and copy[3]. If it matches, then print something.

I tried using IF statements but i keep getting errors.

public static void main(String[] args) {
    String[] copy = {"1","2","1","1","5","6","7","8","9"};
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
user2775042
  • 509
  • 2
  • 6
  • 21

1 Answers1

1

You should use the equals method:

if(copy[0].equals(copy[2]) && copy[0].equals(copy[3])) {
   System.out.printf("%s matches %s",copy[0], copy[1]);
}

if you use the equals operator == you are not comparing the values of the strings, as String is an object you should use the equals to compare the values. The == operator checks if the two references are equals.

Cesar Loachamin
  • 2,740
  • 4
  • 25
  • 33