Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Location
    philippines
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hi Guys, i need help with my simple code, please take a look :)

    Scanner in = new Scanner(System.in);
    String user1 = "admin";
    String pass1 = "admin";
    String user2 = "guest";
    String pass2 = "guest";
    String user3 = "shin";
    String pass3 = "shin";


    System.out.println("Please Enter your Username:");
    String username = in.nextLine();
    System.out.println("Please Enter your Password:");
    String password = in.nextLine();
    if (username.equals("admin") && password.equals("admin")){
    System.out.println("You Have Succesfully Logged-in");
    System.exit(0);
    }

    else if (username.equals("guest")&& password.equals("guest"))
    {
    System.out.println("You have Sucessfully Logged-in");
    System.exit(0);
    }
    else if (username.equals("shin")&& password.equals("shin"))
    {
    System.out.println("You have Sucessfully Logged-in");
    System.exit(0);
    }
    else if (!username.equals(user1)){
    System.out.println("Incorrect Username!");
    }
    else if (!password.equals(pass1)){
    System.out.println("Incorrect Password!");
    }
    else if (!username.equals(user2)){
    System.out.println("Incorrect Username!");
    }
    else if (!password.equals(pass2)){
    System.out.println("Incorrect Password!");
    }
    else if (!username.equals(user3)){
    System.out.println("Incorrect Username!");
    }
    else if (!password.equals(pass3)){
    System.out.println("Incorrect Password!");
    }
    else
    {
    System.out.println("Incorrect username and password");
    }
    }
    }


    im having this error in user 2 and pass 2 also in user3 and pass3

    when i type a wrong username and a correct password it the output is incorrect password while my password is correct, how can i resolve this thank you

    anyways sorry for my bad english its not my current language )
    Last edited by shin; 07-24-2012 at 03:28 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You need to change your datahandling.
    Using if/elseif syntax is performed in order until one condition is satisfied, and if none process the else if available.
    So if I enter shin as a username with no password or the wrong password, the first thing it will do is compare the data to "admin" (with password "admin", but that is skipped), and will fail, then it will evaluate username to "guest" (with password "guest", but that is skipped), and then it evaluates the username to "shin" and the password to "shin" which will fail since the password is incorrect, then it will compare "shin" to "admin" which is false and satisfies that if. Therefore it prints that the username is incorrect.
    To fix, you need to use switch or if/elseif syntax only on the names available, and then nest the password check.
    PHP Code:
    if (username.equals(user1))
    {
        if (
    password.equals(pass1))
        {
            
    System.out.println("This is successful.");
        }
        else
        {
            
    System.out.println("Password incorrect");
        }
    }
    else if (
    username.equals(user2))
    {
        
    //...
    }
    //. . .
    else
    {
         
    System.out.println("Username is incorrect.");

    With hardcoded data like this, I'd use a hashtable for it instead, or a multidimensional array or another collective structure. Programing if/elseif is not ideal for expansion.
    PHP Code:
            java.util.Scanner in = new java.util.Scanner(System.in);
            
    java.util.Map<StringStringhtUsers = new java.util.Hashtable<StringString>();
            
    htUsers.put("admin""admin");
            
    htUsers.put("guest""guest");
            
    htUsers.put("shin""shin");
            
            
    System.out.print("Please Enter your Username:");
            
    String username in.nextLine();
            
    System.out.print("Please Enter your Password:");
            
    String password in.nextLine();
            
            if (
    htUsers.containsKey(username))
            {
                if (
    htUsers.get(username).equals(password))
                {
                    
    System.out.println("Username: " username " and password: " password " are valid");
                }
                else
                {
                    
    System.out.println("Password: " password " is invalid for username: " username);
                }
            }
            else
            {
                
    System.out.println("Username: " username " is invalid.");
            } 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •