Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is wrong with my try catch statement? It will not run properly. PLEASE HELP

ID: 3812633 • Letter: W

Question

What is wrong with my try catch statement? It will not run properly. PLEASE HELP ME, I have spent hours trying to figure this out.

import java.util.Scanner; // Needed for the Scanner class

/**
* This program demonstrates the BankAccount class.
*/

public class AccountTest
{
   public static void main(String [] args)
   {
      BankAccount account;// To reference a BankAccount object
      double balance,        // The account's starting balance
             interestRate,   // The monthly interest rate
             pay,            // The user's pay
             cashNeeded;     // The amount of cash to withdraw
      


      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);
      
      System.out.print("What is your account's starting balance?");


     try
       {
        balance = keyboard.nextDouble();
       }
      catch(NegativeStartingBalance e)
      {
         System.out.println(e.getMessage());
      }
      catch(NegativeInterestRate e)
      {
         System.out.println(e.getMessage());
      }
      
      
      System.out.print("What was your monthly interest rate? ");

       try
       {
             interestRate = keyboard.nextDouble();
       }
      catch(NegativeStartingBalance e)
      {
         System.out.println(e.getMessage());
      }
      catch(NegativeInterestRate e)
      {
         System.out.println(e.getMessage());
      }
      // Create a BankAccount object.
       account = new BankAccount(balance, interestRate);

      // Get the amount of pay for the month.
      System.out.print("How much were you paid this month? ");
      pay = keyboard.nextDouble();

      // Deposit the user's pay into the account.
      System.out.println("We will deposit your pay "
                         + "into your account.");
      account.deposit(pay);
      System.out.println("Your current balance is $"
                         + account.getBalance());

      // Withdraw some cash from the account.
      System.out.print("How much would you like "
                       + "to withdraw? ");
      cashNeeded = keyboard.nextDouble();
      account.withdraw(cashNeeded);

      // Add the monthly interest to the account.
      account.addInterest();

      // Display the interest earned and the balance.
      System.out.println("This month you have earned $"
                         + account.getInterest()
                         + " in interest.");
      System.out.println("Now your balance is $"
                         + account.getBalance());

     
   }
}

Explanation / Answer

HI, You need to add throw statement base on input condition.

I have added required code.

Please let me know in case of any issue.

import java.util.Scanner; // Needed for the Scanner class

/**

* This program demonstrates the BankAccount class.

*/

public class AccountTest

{

   public static void main(String [] args)

   {

       BankAccount account;// To reference a BankAccount object

       double balance, // The account's starting balance

       interestRate, // The monthly interest rate

       pay, // The user's pay

       cashNeeded; // The amount of cash to withdraw

       // Create a Scanner object for keyboard input.

       Scanner keyboard = new Scanner(System.in);

       System.out.print("What is your account's starting balance?");

       try

       {

           balance = keyboard.nextDouble();

          

           if(balance < 0)

               throw new NegativeStartingBalance("negative balance");

       }

       catch(NegativeStartingBalance e)

       {

           System.out.println(e.getMessage());

       }

  

       System.out.print("What was your monthly interest rate? ");

       try

       {

           interestRate = keyboard.nextDouble();

          

           if(interestRate < 0)

               throw new NegativeInterestRate("Negative interest rate");

       }

       catch(NegativeInterestRate e)

       {

           System.out.println(e.getMessage());

       }

       // Create a BankAccount object.

       account = new BankAccount(balance, interestRate);

       // Get the amount of pay for the month.

       System.out.print("How much were you paid this month? ");

       pay = keyboard.nextDouble();

       // Deposit the user's pay into the account.

       System.out.println("We will deposit your pay "

               + "into your account.");

       account.deposit(pay);

       System.out.println("Your current balance is $"

               + account.getBalance());

       // Withdraw some cash from the account.

       System.out.print("How much would you like "

               + "to withdraw? ");

       cashNeeded = keyboard.nextDouble();

       account.withdraw(cashNeeded);

       // Add the monthly interest to the account.

       account.addInterest();

       // Display the interest earned and the balance.

       System.out.println("This month you have earned $"

               + account.getInterest()

               + " in interest.");

       System.out.println("Now your balance is $"

               + account.getBalance());

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote