1

I have a small ATM program and I am using Exception Handling on it. I have a try catch at the start of my program that is going to send it down to the Exception Handling - at the bottom of the program.

The user is suppose to enter a 5 digit number... example: 12345. Then it goes to check the number and I have it checking if the users number is < 5 then send the error but what I want it to do is check if the length is < or > than 5 then send the error. This is my code right now for my program...

import java.util.ArrayList;
import java.util.Scanner;

    public class BankMain
    {
    private double availableBal =80;
    private double totalBal =100;
    private double availableBal2 =480;
    private double totalBal2 =500;
    private double availableBal3 =80;
    private double totalBal3 =100;
    ArrayList<Integer> cardNum = new ArrayList<Integer>();
    static Scanner input = new Scanner(System.in);


    private String error;           //String the error from the exception
    {
        error = "error";
    }


    public void cardNumbers(){


            Scanner cards = new Scanner(System.in);
            Scanner input = new Scanner(System.in);
            Scanner keyboard = new Scanner(System.in);





            try{


            System.out.println("Please select a 5 digit card number");

            cardNum.add(input.nextInt());
            checkNumber();


            }

            catch(invalidNumber err){

                System.out.println("Caught Error: " + err.getError());
            }

            System.out.println("Thank you! You're card number is " +cardNum);

            System.out.println("Type 'c' to go back to main menu.");
            String value = keyboard.next();

            if(value.equalsIgnoreCase("c")){
                menu();

    }

    else if (!keyboard.equals('c')){

        System.out.println("Invalid Entry!");

    }

}


    public void menu(){

    System.out.println("ATM Menu:");
    System.out.println();
    System.out.println("1 = Create Account");
    System.out.println("2 = Account Login");
    System.out.println("3 = Exit ATM");
    query();
                }

        public void startAtm()
        {
            menu();

        }



        public void drawMainMenu()
        {

            AccountMain main3 = new AccountMain();
            int selection;

            System.out.println("\nATM main menu:");
            System.out.println("1 - View account balance");
            System.out.println("2 - Withdraw funds");
            System.out.println("3 - Add funds");
            System.out.println("4 - Back to Account Menu");
            System.out.println("5 - Terminate transaction");
            System.out.print("Choice: ");
            selection =input.nextInt();

            switch(selection)
            {
            case 1:
                viewAccountInfo();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                addFunds();
                break;
            case 4:
                AccountMain.selectAccount();
                break;
            case 5:
                System.out.println("Thank you for using this ATM!!! goodbye");
            }
        }

        public void viewAccountInfo()
        {
            System.out.println("Account Information:");
            System.out.println("\t--Total balance: $"+totalBal);
            System.out.println("\t--Available balance: $"+availableBal);
            drawMainMenu();
        }

        public void viewAccountInfo2()
        {
            System.out.println("Account Information:");
            System.out.println("\t--Total balance: $"+totalBal2);
            System.out.println("\t--Available balance: $"+availableBal2);
            drawMainMenu();
        }

        public void deposit(int depAmount)
        {
            System.out.println("\n***Please insert your money now...***");
            totalBal =totalBal +depAmount;
            availableBal =availableBal +depAmount;
        }

        public void checkNsf(int withdrawAmount)
        {
            if(totalBal -withdrawAmount < 0)
                System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
            else
            {
                totalBal =totalBal -withdrawAmount;
                availableBal =availableBal -withdrawAmount;
                System.out.println("\n***Please take your money now...***");
                }
        }

        public void addFunds()
        {
            int addSelection;

            System.out.println("Deposit funds:");
            System.out.println("1 - $20");
            System.out.println("2 - $40");
            System.out.println("3 - $60");
            System.out.println("4 - $100");
            System.out.println("5 - Back to main menu");
            System.out.print("Choice: ");
            addSelection =input.nextInt();

            switch(addSelection)
            {
            case 1:
                deposit(20);
                drawMainMenu();
                break;
            case 2:
                deposit(40);
                drawMainMenu();
                break;
            case 3:
                deposit(60);
                drawMainMenu();
                break;
            case 4:
                deposit(100);
                drawMainMenu();
                break;
            case 5:
                drawMainMenu();
                break;
            }
        }

        public void withdraw()
        {
            int withdrawSelection;

            System.out.println("Withdraw money:");
            System.out.println("1 - $20");
            System.out.println("2 - $40");
            System.out.println("3 - $60");
            System.out.println("4 - $100");
            System.out.println("5 - Back to main menu");
            System.out.print("Choice: ");
            withdrawSelection =input.nextInt();

            switch(withdrawSelection)
            {
            case 1:
                checkNsf(20);
                drawMainMenu();
                break;
            case 2:
                checkNsf(40);
                drawMainMenu();
                break;
            case 3:
                checkNsf(60);
                drawMainMenu();
                break;
            case 4:
                checkNsf(100);
                drawMainMenu();
                break;
            case 5:
                drawMainMenu();
                break;
            }
        }

public void query(){

    Scanner keyboard = new Scanner(System.in);
    double input = keyboard.nextInt();  


    if (input == 2){

        BankMainPart2 main2 = new BankMainPart2();

        System.out.println("Please enter your 5 digit card number.");
         BankMainPart2.loginCard(cardNum);



    }
    else if (input == 1){

        cardNumbers();

    }

    else if (input == 3){
        System.out.println("Thank you, have a nice day!");
        System.exit(0);


    }

}

public void checkingMenu()
{

    AccountMain main3 = new AccountMain();
    int selection;

    System.out.println("\nATM main menu:");
    System.out.println("1 - View account balance");
    System.out.println("2 - Withdraw funds");
    System.out.println("3 - Add funds");
    System.out.println("4 - Back to Account Menu");
    System.out.println("5 - Terminate transaction");
    System.out.print("Choice: ");
    selection =input.nextInt();

    switch(selection)
    {
    case 1:
        viewAccountInfo2();
        break;
    case 2:
        withdraw();
        break;
    case 3:
        addFunds();
        break;
    case 4:
        AccountMain.selectAccount();
        break;
    case 5:
        System.out.println("Thank you for using this ATM!!! goodbye");
    }
}

private static void checkNumber() throws invalidNumber              //run the check activation exception
{
    if(input.nextInt() == 5)
  {
        System.out.println("Works!");
  }
    else{

     throw new invalidNumber("invalid number");
 }  
}

    public static void main(String args[])
    {
        BankMain myAtm = new BankMain();
        myAtm.startAtm();
    }
}

4 Answers 4

1

Convert number to String and then use length() to check the number of digits

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the reply, sorry I'm new to java and I'm not sure exactly how to do that
String.valueOf(yourIntVariable).length()!=5{raise exception}
1

As it is, your program should hang (you are not specifying this in your question so my assumption might be incorrect). You are calling input.nextInt() twice, which will make your program expect two sets of digits from your user.

To go around this, I would recommend that your first read the input of the user as a string, using the nextLine() method. Then, you will do something like so:

String userInput = input.nextLine().trim();
checkNumber(userInput);
cardNum.add(Integer.parseInt(userInput);

Also, the checkNumber, as is, will fail since we are now reading the entire line, so you will have to make the following changes:

  • You will have to iterate over your characters and use the isDigit to make sure that each of your characters is a digit OR

  • User a regular expression of the sort ^\\d{5}$ to make sure that the string is made up entirely from 5 numbers. For regular expressions, you can take a look at this tutorial to get you on the right track. If you are a beginner, this might seem a bit overwhelming but regular expressions are usually recommended to do data validation on strings.

Comments

1
        System.out.println("Please select a 5 digit card number");

        String s=input.next();

        //Here check the nu. digits by using length()

        cardNum.add(s);

        checkNumber();

1 Comment

alright I did all this and now I use s.length() == 5 at the bottom but it wont read the string s .
0

You need to get the entire code then check it length if its == 5 or not, you will need to convert the input to string using:

String.valueOf(int i)

or

Integer.toString(int i)

or just read it as string using:

string givenCode = yourScan.next();

then check the length:

if (givenCode.length() == 5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.