Hello I'm currently working with my Java program here is my code:

import java.util.Scanner;
    public class StringAct{
        public static void main(String[]args){
            Scanner inp=new Scanner(System.in);


                System.out.print("Enter string: ");
                String a= inp.nextLine();

                if(a=="angel"){
                    System.out.print("Valid");
                }
                else{
                    System.out.print("Not Valid");
                }
            }
        }

this program asks the user to enter a string, the string that the user should enter is equivalent to "angel" to display valid. If the user types other word it should display not valid and that's my problem with this code. Everytime I type angel it still displays not valid please help me with this thing. Thanks.

Recommended Answers

All 3 Replies

I'm not a java person, I don't use it but I can suggest to first try to print out your input to see what it is, maybe trim whitespace? Just some suggestions.

that is because you are comparing your Strings in a bad way.
'==' will compare the values of primitives correctly, but for Objects (and String is an object) '==' will compare references, not equality of the value.

use the .equals() or .equalsIgnoreCase() method instead.

that is because you are comparing your Strings in a bad way.
'==' will compare the values of primitives correctly, but for Objects (and String is an object) '==' will compare references, not equality of the value. use the .equals() or .equalsIgnoreCase() method instead.

Thanks man! I appreciate the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.