Reading and checking strings with 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 click yes, it should say

"I programmed this right!"

but he says

" Awww: ( "

+4
source share
6 answers

You are not comparing strings correctly. You should use the equals() method, for example:

 if (answer.equals("yes")) 

When you program in Java, the == operator is usually used to compare primitive data types ( int , double , etc.). If you use == to compare two types of objects (for example, strings), you compare them for identification, that is, you check to see if they refer to one object in memory. In your case, you need to compare if they are equal: if they have the same value (a string of characters in this case), even if they are two different objects, and for this you should use equals() .

EDIT:

Even better, to prevent a NullPointerException , it is considered good practice to reverse the comparison order and first write the line with which you are comparing, for example:

 if ("yes".equals(answer)) 

The explanation is simple: if for some reason the answer is null , the above comparison will be evaluated to false (which means: answer not "yes" ), while the first version of the code will call a NullPointerException when trying to call the equals() method for null .

+9
source
 if(answer == "yes"){ 

it should be

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

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

+2
source

Use String.equals() instead of == .

In Java, == checks that 2 lines are the same instance, where "a"! = "A". Instead, you need to check "a".equals("a") .

So replace

 if(answer == "yes"){ 

with:

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

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

+1
source

Change it

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

The equals() method compares this string (the answer in your example) with 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 already mentioned, the equals() method compares characters inside a String object. The == operator compares two object references to see if they refer to the same instance.

+1
source

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 :("); } 

}}

0
source

or you can try using the 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 :("); } } 
0
source

Source: https://habr.com/ru/post/1388378/


All Articles