Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Don't understand why "if else" don't work.

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Don't understand why "if else" don't work.

    I just don't understand why "if" statement always get "false". Can someone explain me it, pls?


    import java.util.Scanner;
     
    public class Test 
    {
        public static void main(String[] args) 
    {
            String b, c;
            Scanner scanText = new Scanner(System.in);
            System.out.print("Enter key word: ");
            b = scanText.next();  //typing "key"
            System.out.print("Enter key word once again: ");
            c = scanText.next(); //once again typing "key"
            if(c == b)
                System.out.println("Access allowed.");
            else
                System.out.println("Access denied.");
        }
    }

    Why I can't reach "Access allowed."?
    Not if(c == b), nor if("key" == b) or if( (c="key") == b).

    Thank you in advance for your help.
    Last edited by Brume; November 15th, 2019 at 11:19 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Don't understand why "if else" don't work.

    Use the equals method to compare the contents of String objects, not the == operator.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Don't understand why "if else" don't work.

    Thank you for answering.
    Actually, not long before I found out also, that, for example if (c == b), it's compares addresses, not actual data.

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  2. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  3. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  4. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  5. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM