0
import java.util.Scanner;

public class teststuff {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);      
        String test;
        test = scan.next();

        if(test == "example"){
            System.out.println("it worked");
        }
    }
}

Here's the code, and my input is "example" even though it's exactly the same it still doesn't give me a boolean true for my if statement. Is there something I'm doing wrong?

ajkey94
  • 411
  • 2
  • 10
  • 19

2 Answers2

2

You need to use:

test.equals("example")

Because you are checking if the objects are equal, not if the object values are equal.

This is a pretty common mistake, but thankfully it has a very simple fix. :)

Stephen Buttolph
  • 643
  • 8
  • 16
0

mainly in java we don't compare String with == operator since String is class we use equals method:

test.equals("");

it returns true if the two strings are equal and false if not