How to separate this two? i mean 2 different account

The "Invalid Username or Password" Always Pop-up.

What i'm trying to do is, two accounts must successfully login, but the error is i cant login the second account. Please lend me some help! what am i lacking ? Some suggestions? Please help me!

import java.io.*;
public class Sample {
public static void main (String[] Ropher) throws IOException
{
    BufferedReader br = new BufferedReader ( new InputStreamReader (System.in));
    String username;
    String password;

    String user1 = "Geoddie";
    String user2 = "Rosiel";
    String pass1 = "123";
    String pass2 = "914";

    int j = 1;
    int x = 1;

    System.out.print("Username: ");
    username = br.readLine();
    System.out.print("Password: ");
    password = br.readLine();

    while (x <=3)
    {


    if ((username.equals(user1)) && (password.equals(pass1))){
        System.out.println("Welcome!");
        break;
    }else if (!(username.equals(user1)) && !(password.equals(pass1))){
        System.out.println("Invalid Username or Password!");

    }else if (!(username.equals(user1))){
        System.out.println("Invalid Username!");

    }else if (!(password.equals(pass1))){
        System.out.println("Invalid Password!");
        x++;
        //
        //
        //

        }
    }

    while (j<=3){



     if ((username.equals(user2)) && (password.equals(pass2))){
        System.out.println("Welcome!");

        break;


    }else if (!(username.equals(user2))){
        System.out.println("Invalid Username!");

    }else if (!(password.equals(pass2))){
    System.out.println("Invalid Password!");

    }
    else if (!(username.equals(user2)) && !(password.equals(pass2))){
        System.out.println("Invalid Username or Password!"); 
    j++;
            }
        }
    }
}

Upvotes: 0

Views: 86

Answers (3)

Makudex
Makudex

Reputation: 1082

If you want a fully working log-in then you should do this:

import java.io.*;
public class Sample {
public static void main (String[] Ropher) throws IOException
{
    BufferedReader br = new BufferedReader ( new InputStreamReader (System.in));
    String username;
    String password;

    String user1 = "Geoddie";
    String user2 = "Rosiel";
    String pass1 = "123";
    String pass2 = "914";

    int x = 1;

    while (x <=3)
    {

        System.out.print("Username: ");
        username = br.readLine();
        System.out.print("Password: ");
        password = br.readLine();


        if ((username.equals(user1)) && (password.equals(pass1))){
            System.out.println("Welcome! "+user1);
            break;
        }else if ((username.equals(user2)) && (password.equals(pass2))){
            System.out.println("Welcome! "+user2);
            break;
        }else if (!(username.equals(user1)) && !(password.equals(pass1)) || !(username.equals(user2)) && !(password.equals(pass2))){
            System.out.println("\nInvalid Username or Password!");
            System.out.println("You have "+(3-x)+" login attempts left.");
            if ((3-x) == 0)
            {
                System.out.println("Goodbye");
            }
            System.out.println();
            x++;
        }
    }

    }
}

Explanation:

What i did in this code is that

1. the program prompts the user for their username and password

2. if the username and password matched it will display a message "Welcome"

3. else if it's not found displays a message "Invalid username and password"

4. and then shows how many remaining login attempts left

5. and if it reaches the maximum attempts which is 3 then it will end the program and displays "Goodbye".

Hope this helps.

Upvotes: 0

Harshit
Harshit

Reputation: 5157

How could you login to your second account ?

What you are doing in second comparison is

user2 = "Rosiel"
username = "Geoddie"

You are trying to compare user2.equals(username). Since you first got successfully logged in so your username variable contains the value Geoddie.

Now you are using the same username to match user2 which contains the value Rosiel.

You should also put

    System.out.print("Username: ");
    username = br.readLine();
    System.out.print("Password: ");
    password = br.readLine();

before

while (j<=3){

Upvotes: 1

Alvaro Pedraza
Alvaro Pedraza

Reputation: 1292

Some observations:

  1. As TAsk said, the readLine() must be inside the loop
  2. The condition !(username.equals(user1)) && !(password.equals(pass1)) IS NOT equal to !(username.equals(user1) && password.equals(pass1)) (the negation of the success condition). If you want to make a logic negation you must change the connector also, that would be: !(username.equals(user1) && password.equals(pass1)) = !username.equals(user1) || !password.equals(pass1) (change AND by OR). Note that your message is "Invalid Username OR password"
  3. The sentence x++ is just in one condition. For the first validation, you are incrementing the counter only when you have incorrect password. x++ must be OUTSIDE the conditional structure.

Review your code with this suggestions and let us know how it goes. Hope this helps. Best regards

Upvotes: 1

Related Questions