Home > database >  I want to sign in using the username and pin i have registered in arraylist but it keeps going wrong
I want to sign in using the username and pin i have registered in arraylist but it keeps going wrong

Time:05-11

Why is wrong in my code? I want to sign in using the username and pin i have registered in arraylist but it keeps going wrong (JavaOOp)

static void registerAccount(){
    Scanner input = new Scanner(System.in);

    System.out.println("Register Your Account");

    System.out.print("-----Enter your username: ");
    String name = input.nextLine();
    
    System.out.print("-----Enter your desired 4-digit pin: ");
    String pin = input.next();
      
    System.out.print("-----Enter your first deposit: ");
    int balance = input.nextInt();
    
    userList.add(new account(name,pin,balance));
    
    System.out.println("-----Register Successful-----");
    
    signInAccount();
    
}

static void signInAccount(){
    Scanner input = new Scanner(System.in);
    

    System.out.println("-----Log In Your Account-----");
    
    System.out.print("-----Enter your Username: ");
    String cname = input.next();
    
    System.out.print("-----Enter your Pin: ");
    String cpin = input.next();
    
    if (cname.equals("") && cpin.equals("")){
        System.out.println("No credentials entered");
    } else {
        for (int i=0; i< userList.size();i  ){
            if (userList.get(i).equals(cname) && userList.get(i).equals(cpin)){
                System.out.println("LogIn Successful");
            } else {  
                System.out.println("Wrong");
                signInAccount();
            }
        }
    }
}

i'm still trying to learn oop

CodePudding user response:

Here is your current sing in method:

static void signInAccount(){
    Scanner input = new Scanner(System.in);   
    System.out.println("-----Log In Your Account-----");
    System.out.print("-----Enter your Username: ");
    String cname = input.next();
    System.out.print("-----Enter your Pin: ");
    String cpin = input.next();
    
    if (cname.equals("") && cpin.equals("")){
        System.out.println("No credentials entered");
    } else {
        for (int i=0; i< userList.size();i  ){
            if (userList.get(i).equals(cname) && userList.get(i).equals(cpin)){
                System.out.println("LogIn Successful");
            } else {  
                System.out.println("Wrong");
                signInAccount();
            }
        }
    }
}

I'll go over a few things you are doing wrong here, which will no doubt attribute to, if not cause your issue.

First: don't create a Scanner instance for each method, create it on class level and use it in each method. But this is not really relevant for this, so I'll leave that be.

Now, let's assume in your arrayList, you have the following elements:

  • "John", "id_01"
  • "Tom", "id_02"

And, you try to login with "Tom".

static void signInAccount(){
    Scanner input = new Scanner(System.in);   
    System.out.println("-----Log In Your Account-----");
    System.out.print("-----Enter your Username: ");
    String cname = input.next();
    System.out.print("-----Enter your Pin: ");
    String cpin = input.next();
    
    if (cname.equals("") && cpin.equals("")){ // doesn't match your data, go to else
        System.out.println("No credentials entered");
    } else {
        for (int i=0; i< userList.size();i  ){
            if (userList.get(i).equals(cname) && userList.get(i).equals(cpin)){ // you first compare to "John", so go to else
                System.out.println("LogIn Successful");
            } else {   // you automatically print "wrong", and again call the method, and you re-enter the information for "Tom"
// But, when you do so, it will re-start at the beginning of the list, comparing to "John", coming in this else, and ...
                System.out.println("Wrong");
                signInAccount();
            }
        }
    }
}

So: First enhancement: do NOT recursively call your method (or at least not in this else block)

static void signInAccount(){
    Scanner input = new Scanner(System.in);   
    System.out.println("-----Log In Your Account-----");
    System.out.print("-----Enter your Username: ");
    String cname = input.next();
    System.out.print("-----Enter your Pin: ");
    String cpin = input.next();
    
    if (cname.equals("") && cpin.equals("")){
        System.out.println("No credentials entered");
    } else {
        for (int i=0; i< userList.size();i  ){
            if (userList.get(i).equals(cname) && userList.get(i).equals(cpin)){
                System.out.println("LogIn Successful");
            } else {  
                System.out.println("Wrong");
                // signInAccount(); -> with this out of the way, your code will first print "Wrong", then "LogIn Successful"
            }
        }
    }
}

Now, we encounter a new problem. Why does it still print wrong? And, what happens if there are other elements after "Tom" in your list?

So, assume your list has:

  • "John", "id_01"
  • "Tom", "id_02"
  • "Samantha", "id_03"

And you go looking for "Tom" with "id_02", you would get Wrong LogIn Successful Wrong

For each other person you add in your list, you would get a new "Wrong", which is clearly not what you want.

So, use a flag to check whether or not you found the person.

static void signInAccount(){
    Scanner input = new Scanner(System.in);   
    System.out.println("-----Log In Your Account-----");
    System.out.print("-----Enter your Username: ");
    String cname = input.next();
    System.out.print("-----Enter your Pin: ");
    String cpin = input.next();
    
    if (cname.equals("") && cpin.equals("")){
        System.out.println("No credentials entered");
    } else {
        boolean found = false;
        for (int i=0; i< userList.size();i  ){
            if (userList.get(i).equals(cname) && userList.get(i).equals(cpin)){
                found = true;
                break; // jump out of the loop, no need to check the rest
            }
        }
        if ( found ) System.out.println("Successful login");
        else System.out.println("Login failed");
    }
}

Another option is to break out of the method once you found the right person:

static void signInAccount(){
    Scanner input = new Scanner(System.in);   
    System.out.println("-----Log In Your Account-----");
    System.out.print("-----Enter your Username: ");
    String cname = input.next();
    System.out.print("-----Enter your Pin: ");
    String cpin = input.next();
    
    if (cname.equals("") && cpin.equals("")){
        System.out.println("No credentials entered");
    } else {
        for (int i=0; i< userList.size();i  ){
            if (userList.get(i).equals(cname) && userList.get(i).equals(cpin)){
                System.out.println("Login succesful");
                return; // don't try to return a value, since your method is void
            }
        }
        System.out.println("Login failed");
    }
}

CodePudding user response:

It would be easier to make a separate User class in which to make the Name, Pin, Balance fields and make a method that will check the Name and Pin for correctness, after which the method will display data about the User

  • Related