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

Thread: What is wrong with my code???

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code???

    I have problem with my if statement. Even I input user still it is going to else statement. Can some explain to whats is the problem with my code... Thanks...

    import java.util.Scanner;
    class Login {
    	public static void main(String[] args){
    		Scanner input = new Scanner(System.in);
    		String user, pass;
    		System.out.println("Enter your Username: ");
    		user = input.nextLine();
    		System.out.println("Enter your Password: ");
    		pass = input.nextLine();
     
    		if (user == "user" && pass == "admin")
    			System.out.println("Access granted!!!");
    		else
    			System.out.println("Access denied!!!");
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,261
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: What is wrong with my code???


  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,616
    Thanks
    25
    Thanked 321 Times in 299 Posts

    Default Re: What is wrong with my code???

    Welcome to JAVA 101! We must get this question like 5 times a day on this forum.

    Basically, Strings (which is what user and pass are) should be compared using one of the String comparing methods. Specifically, if you want to check two Strings for the same value (case-sensitive), use the String.equals(String) method. If you want to check two Strings for the same value (not case-sensitive), use the String.equalsIgnoreCase(String) method.

    The use of the == sign is telling the program to check the two Strings to see if they share the same spot in memory, not to compare their values.

    Similarly, to compare any objects for the same value, you should use the Object.equals(Object) method, not the == sign.

    The only case where you should use the == sign to check value is with simple-type numbers (such as ints, double, floats, ect.).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    c.P.u1 (February 4th, 2011)

  5. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code???

    Thanks aussiemcgr...

    import java.util.Scanner;
    class Login {
    	public static void main(String[] args){
    		Scanner input = new Scanner(System.in);
    		String user, pass;
    		System.out.println("Enter your Username: ");
    		user = input.nextLine();
    		System.out.println("Enter your Password: ");
    		pass = input.nextLine();
     
    		if (user.equals("user") == true && pass.equals("pass") == true)
    			System.out.println("Access granted!!!");
    		else
    			System.out.println("Access denied!!!");
    	}
    }

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: What is wrong with my code???

    You don't need to have:

    if (user.equals("user") == true && pass.equals("pass") == true)

    This will do:

    if (user.equals("user") && pass.equals("pass"))
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 04:21 AM
  2. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 02:38 AM
  3. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 02:24 PM
  4. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM
  5. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 01:44 PM