Cole
Cole

Reputation: 2815

Reading and checking strings from user input

I have this code:

import java.util.Scanner;

public class Example {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String answer = input.nextLine();

    if(answer == "yes"){
        System.out.println("Yea I programmed this right!");
    }else{
        System.out.println("Awww :(");
    }
  }
}

But when I run it and type yes, it should be saying

"Yea I programmed this right!"

but it says

"Awww :("

Upvotes: 6

Views: 56818

Answers (6)

merve
merve

Reputation: 1

or you can try to use "compareTo()" function

private static Scanner input;
private static String choice;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    input = new Scanner(System.in);
    choice = input.nextLine();

    if (choice.compareTo("yes") == 0) {
        System.out.println("Yea I programmed this right!");
    } else {
        System.out.println("Awww :(");
    }

}

Upvotes: 0

ramesh
ramesh

Reputation: 1

import java.util.Scanner;

public class Example { public static void main(String[] args) {

Scanner input = new Scanner(System.in);
String answer = input.nextLine();

/*Edit your next line as mine,u'll get the correct ans...*/

if("yes".equals(answer)){ 
    System.out.println("Yea I programmed this right!");
}else{
    System.out.println("Awww :(");
}

} }

Upvotes: 0

Matthew Gilliard
Matthew Gilliard

Reputation: 9498

if(answer == "yes"){

should be

if("yes".equals(answer)){

(== is not correct for String equality, and we handle the case where answer is null)

Upvotes: 2

Óscar López
Óscar López

Reputation: 236140

You're comparing strings incorrectly. You must use the equals() method, like this:

if (answer.equals("yes"))

When you're programming in Java, the operator == is generally used for comparing primitive data types (int, double, etc.). If you use == for comparing two object types (like strings), you're comparing them for identity, that is, checking if they reference the same object in memory. In your case, what you need is to compare if they're equal: if they have the exact same value (a string of characters in this case) even if they're two different objects - and for that you must use the equals() method.

EDIT :

Even better, for preventing a NullPointerException, it's considered a good practice flipping the order of the comparison and writing first the string you're comparing with, like this:

if ("yes".equals(answer))

The explanation is simple: if for some reason answer is null, the above comparison will evaluate to false (meaning: answer is not "yes"), whereas the first version of the code would cause a NullPointerException when trying to call the equals() method on a null value.

Upvotes: 12

Lion
Lion

Reputation: 19037

Change this

if(answer.equals("yes")){
    System.out.println("Yea I programmed this right!");
}else{
    System.out.println("Awww :(");
}

The equals() method compares this string (answer in your example) to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

It is important to understand that the equals() method and the == operator perform two different operations. As just mentioned, the equals() method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance.

Upvotes: 1

ziesemer
ziesemer

Reputation: 28707

Use String.equals() instead of ==.

In Java, == is testing that the 2 Strings are the exact same instance, where "a" != "a". Instead, you need to test for "a".equals("a").

So replace

if(answer == "yes"){

with:

if("yes".equals(answer)){

Note that flipping the order here is intentional, as this can prevent a NullPointerException if answer was null - as "yes".equals(null) will simply return false, instead of throwing an exception. (Calling an operation on null would throw a NullPointerException, I.E. null.equals("yes").)

Upvotes: 1

Related Questions