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: Super new at Java

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

    Default Super new at Java

    Hey, I'm just learning java I wrote this super simple program. I encountered a bug that I fixed but don't know why it fixed it and am trying to understand why, on top of the countless other mistakes I've probably made. If you remove () from
    if((name.equalsIgnoreCase(username) ||name.equalsIgnoreCase(username2)) && pass.equalsIgnoreCase(password))
    to look like
    if(name.equalsIgnoreCase(username) || name.equalsIgnoreCase(username2) && pass.equalsIgnoreCase(password))
    for the first username "brennan" you can input any password but for the username2 "Chip" it works, and putting () fixes it.

    public class New {
        static Scanner sc = new Scanner(System.in);
        public static void main(String []args){
            boolean click = true;
            String username = "Brennan";
            String username2 = "Chip";
            String name;
            String password = "Let me in";
            String pass;
            do{
                System.out.println("Username: ");
                name = sc.nextLine();
                System.out.println("Password: ");
                pass = sc.nextLine();
                if((name.equalsIgnoreCase(username) || name.equalsIgnoreCase(username2)) && pass.equalsIgnoreCase(password)){
                    System.out.println("Correct!");
                    sc.close();
                    break;
                }else if(!name.equals(username) && !pass.equals(password)){
                    System.out.println("Incorrect username or password!");
                }
            }while(click);
        }
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Super new at Java

    Without the parentheses, the evaluation order is strictly left to right. Once you get a valid first user name, the rest is not evaluated because if the first part is true, the rest is irrelevant so the if statement short circuits. When you type in a valid second user name, the && comes into play so a valid password is required.

    When you group the first two statements, one or the other must be true at which point the && becomes important because if it is false, the if statement false, if true, the if statement is true.

    In the last case, if either username is invalid, the the && short circuits since (false || false) is false and thus the last expression is not necessary to evaluate.

    	  System.out.println(true || false && false); // true
    	  System.out.println(true || false && true);  // true
    	  System.out.println(false || true && true);  // true
    	  System.out.println(false || true && false); // false
    	  System.out.println((true || false) && false); // false
    	  System.out.println((true || false) && true); // true
    	  System.out.println((false || true) && true); // true
    	  System.out.println((false || true) && false);// false

    Regards,
    Jim
    Last edited by jim829; May 14th, 2019 at 04:23 PM.

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

    Default Re: Super new at Java

    Ah ok I see, thank you.

Similar Threads

  1. keyword super in java
    By shirin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 27th, 2013, 05:23 AM
  2. Super Simple?
    By sglennlmb in forum Java Theory & Questions
    Replies: 0
    Last Post: April 3rd, 2013, 12:50 AM
  3. please help super new to Java
    By R.Rivera in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 13th, 2011, 07:58 PM
  4. Super Friendly & Nice Java Forum!
    By friendly_jessie21 in forum Member Introductions
    Replies: 2
    Last Post: February 5th, 2011, 03:28 PM
  5. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM