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 4 of 4

Thread: If condition not working.

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default If condition not working.

    Hello, i'm trying to make a program that checks for your username and password which are stored in strings, it seems to take the else route everytime i enter the username right though.
    import java.util.Scanner;
    public class passwordCheck {
    	public static void main(String args[]){
    		String password = "test123";
    		 String username = "variumzky";
    		  Scanner check = new Scanner(System.in);
    		   System.out.println("Enter your username here:");
    		     if (check.nextLine() == username){
    		    	 System.out.println("Enter your password");
    		     }else{
    		    	 System.out.println("No username like that was recognized in our database");
    		    	 check.close();
    		     }
     
    		    }
    	}


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: If condition not working.

    hello,
    try the equal method instead of == operator and see what you get which in your case:
    if(check.nextLine().equals(username))
    {...}

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: If condition not working.

    Thank you sir!

    --- Update ---

    Quote Originally Posted by Alaa View Post
    hello,
    try the equal method instead of == operator and see what you get which in your case:
    if(check.nextLine().equals(username))
    {...}
    I'm still kinda curious though, why didn't the == operator work?

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: If condition not working.

    hello,
    think of check.next() and username are different objects since String objects are immutable. you cant compare two objects by == operator however java team provided the method equals to compare their values instead of the actual objects. that said, with == operator you compare between the two String objects while with equals you are comparing their values if they are the same. helpful?

  5. The Following User Says Thank You to Alaa For This Useful Post:

    Variumzky (July 29th, 2013)

Similar Threads

  1. [SOLVED] Conditionals with more than one condition.
    By mstabosz in forum Java Theory & Questions
    Replies: 4
    Last Post: July 12th, 2013, 06:11 AM
  2. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  3. condition number? what is it?
    By gspease839 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 9th, 2013, 10:00 PM
  4. condition number help?
    By gspease839 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 9th, 2013, 07:40 PM
  5. Waiting until condition
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2010, 09:24 AM