Function not getting called/throwing exception in Java code

52 Views Asked by At
import java.util.*;
public class oops{
    public static void main(String args[]){
        System.out.println("Enter the username you want to set the for your Bank Account");
        Scanner sc2=new Scanner(System.in);
        String user=sc2.nextLine();
        Bank_account ba=new Bank_account();
        ba.username=user;
        //ba.set_pw();----not possible directly so i created a class to authenticate.
        ba.authenticate();
        //ba.display_password();-----again not possible as it is also private.
        ba.show_password();
        sc2.close();


    }
}
class Bank_account{
    public String username;
    private int password;
    public int dig_lock=123;
    private void set_pw(int s){
        this.password=s;
    }




    public void authenticate(){
        int lock;
        int count=3;
        Scanner sc1=new Scanner(System.in);

        while(count>0){
            System.out.println("Enter the lock if want to set the password "+username);
            lock=sc1.nextInt();
            sc1.nextLine();
            boolean allow=(dig_lock==lock)?true:false;
            if(allow){
                System.out.println("Enter the paassword you want to set "+username);
                int s1=sc1.nextInt();
                set_pw(s1);
                break;
            }
            count--;
            System.out.println("Sorry try again to aunthenticate "+username);
        }
        if(count<=0){
            System.out.println("Failed to authenticate ,sorry you cant set the password "+username);
        }
        sc1.close();
    }




    public void show_password(){
        int count=3;
        int lock;
        Scanner sc=new Scanner(System.in);
       
        
        while(count>0){
            System.out.println("Enter the lock if u want to access the password "+username);
            lock=sc.nextInt();
            sc.nextLine();
            boolean allow=(dig_lock==lock)?true:false;
            if(allow){
                display_password();
                break;
            }
            count--;
            System.out.print("Sorry wrong passwrod Try again "+username);

        }
        if(count<=0){
            System.out.print("You have used ur all terns,sorry can't show password "+username);
        }
        sc.close();
    }
    private void display_password(){
        System.out.print(password);

    }


}

it is code for banking system where user have to authenticate before setting and displaying its password by entering a predefined lock which should match dig_lock

Function named set_pw is not working as expected in this code and throwing some error and also in authenticate function ,even after entering correct lock it should break the loop but its still asking for the lock to be entered by the user but it should break and just call for the set_pw please solve this earlier also i asked this but no response was there.--------The error is not mentioned even in vscode its just showing Error in thread main. please help me to solve this.

1

There are 1 best solutions below

0
Grinding For Reputation On
        ...
49      if(count <= 0) {
50          System.out.println("Failed to authenticate ,sorry you cant set the password " + username);
51      }
52      sc1.close(); // Cause of the issue
53  }

...

You're closing the Scanner which means you can't get input from System.in which is generally a bad practice. If you're interested you can read more here.


Also naming conventions are a good practice and makes your code look good.