Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:43:51+00:00 2026-06-11T22:43:51+00:00

I made an ATM program. I have a try catch that will ask for

  • 0

I made an ATM program. I have a try catch that will ask for the user to type in their pin number. The pin number must be 5 digits. So the exception will check if it is 5 digits or not but the exception handling is not working. No matter what number I type in, it always says invalid number.

Here is my code the try catch is at the top of the program and the exception handling checkNumber is at the bottom of the 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 (String.valueOf(input).length() != 5)
        {
            throw new invalidNumber("invalid number");
        }
        else
        {

            System.out.println("Works!");
        }

    }

    public static void main(String args[])
    {
        BankMain myAtm = new BankMain();
        myAtm.startAtm();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:43:52+00:00Added an answer on June 11, 2026 at 10:43 pm

    Why you are having three different instances of Scanner class.. That is what making the program confusing..

    In the compareNumber() method, you are actually checking the value of input, which is an instance of Scanner.. It’s better to use it like this: –

    checkNumber(input.nextInt())

    And add the number to the list in your checkNumber(int num) method..

    Of course I am not saying that it is a good way of coding.. But it will solve your problem for the time being..

    Else, there are so many issues with your code..

    This is your try-catch block: –

            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());
            }
    

    And this is your checkNumber() method : –

    private static void checkNumber() throws invalidNumber 
    {
        if (String.valueOf(input).length() != 5) {
            throw new invalidNumber("invalid number");
        }
        else {
            System.out.println("Works!");
        }
    }
    

    Now you must see that you are using input as a parameter to String.valueOf(input).

    But you have declared ‘input` as an instance of Scanner before your try-catch block..

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

    This code is in your codeNumbers() method..

    So, clearly your input can never actually contain a user input, rather a hashcode representing the object new Scanner(System.in).

    So, its better that you pass the integer input from user to checkNumber() method..

    ************ MODIFICATION Needed in Code..

    So, your checkNumber()` will be modified as: –

        private static void checkNumber(int number) throws invalidNumber 
        {
            if (String.valueOf(number).length() != 5) {
                throw new invalidNumber("invalid number");
            }
            else {
                System.out.println("Works!");
            }
        }
    

    And your call to this method in try-catch block will change to this: –

            try {
                System.out.println("Please select a 5 digit card number");
                int number = input.nextInt();
                cardNum.add(number);
                checkNumber(number);
            } catch (invalidNumber err) {
                System.out.println("Caught Error: " + err.getError());
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a UITableViewCell and I have 20 rows in that table in which
We have WebRole, that hosting multiple sites. We made startup tasks for each site
I made a Windows Forms application that calculates the factorial of a number. All
I'm working on a bigger project atm, but I made this simple example to
I made an iphone app to capture image from camera and to set that
I made a Web service in which I have a function to count some
I made an application that passes trough an XML file and extracts the entries
I made a little PHP script that checks if an email is valid. The
Made a fiddle for this: http://jsfiddle.net/terjeto/MN4FJ/ My problem is that dragleave fires when you
made a short java program in Bluej upon compiling it and trying to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.