i dont know y but my driver wont work. i cant get a= getBalance(). if i create an object like CheckingAccount jjj = new CheckingAccount my scanner wont work. could you hep me fix this problem and also so when i input a negative number it prints out the error from my class.

this is my driver

import java.util.*;
public class CheckingAccountDriver
{
      public static void main(String args[])      
      {
             
             Scanner keyboard = new Scanner(System.in);
             System.out.print("Please enter the starting Balance --> ");
             double a = keyboard.nextDouble();
             a = getBalance();
             System.out.println("Account opened with a balance of "+ a);
             System.out.print("Please enter the amount of deposit-->");
             double b = keyboard.nextDouble();
             System.out.println("Deposit made. Current account balance =  ");
             CheckingAccount lol = new CheckingAccount(a, 0); 
            }
        }

& my Class

public class CheckingAccount
{
  private double myBalance;
  private int myAccountNumber;

  public CheckingAccount()
  {
    myBalance = 0.0;
    myAccountNumber = 0;
  }

  public CheckingAccount(double initialBalance, int acctNum)
  {
    if (initialBalance < 0)
    throw new IllegalArgumentException("Error: Initial balance cannot be negative");
    else
    {
        myBalance = initialBalance;
        myAccountNumber = acctNum;
    }
   }

  public double getBalance()
  {
    return myBalance;
  }

  public void deposit(double amount)
  {
        if (amount < 0)
        throw new IllegalArgumentException("Error: amount cannot be negative");
        else
         myBalance += amount;
  }

  public void withdraw( double amount )
  {
        if (amount > myBalance)
        throw new IllegalArgumentException("Error: amount cannot be withdrawn");
        else
        myBalance -= amount;
  }
}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

i fixed this up so it works however when i enter in a negative # It Doesnt tell the user it wont work. any sugestions?

i fixed this up so it works however when i enter in a negative # It Doesnt tell the user it wont work. any sugestions?

Sounds like you want an if statement. If input is less than zero, print some sort of warning, otherwise go ahead and execute the program logic.

so my code is involving exclusions , try & catch. WHen i run my code, it produces the error message however it then skips to the next step. for example, when it asks how much do i want to deposit, if the user enters in a negative # it will produce the error message but then it will go to the next step. i need for it to ask the question again until the user enters in a valid number. any advice would be really helpful

my class

 import java.util.*;
public class Check
{
  private double myBalance;
  private int myAccountNumber;

  public Check()
  {
    myBalance = 0.00;
    myAccountNumber = 0;
  }

  public Check(double initialBalance, int acctNum)
  {
    if (initialBalance < 0)
    throw new IllegalArgumentException("Error: Initial balance cannot be negative");
    else
    {
        myBalance = initialBalance;
        myAccountNumber = acctNum;
    }
   }


   public double getBalance()
  {
    return myBalance;
  }

  public void deposit(double amount)
  {
        if (amount < 0)
        throw new IllegalArgumentException("Error: amount cannot be negative");
        else
         myBalance += amount;
  }

  public void withdraw( double amount )
  {
        if (amount > myBalance)
        throw new IllegalArgumentException("Error: amount cannot be withdrawn");
        else
        myBalance -= amount;
  }

  public void input()
  {
     Scanner keyboard = new Scanner(System.in);
     System.out.print("Please enter the starting Balance --> ");
     try{
     double ho = keyboard.nextDouble();
          myBalance = ho;
    }
    catch(IllegalArgumentException e)
    {
    System.out.println(e.getMessage());
    }

     System.out.println("Account opened with a balance of "+myBalance);
     System.out.print("Please enter the amount of deposit-->");
     try{
     double lo = keyboard.nextDouble();
     deposit(lo);
      }
    catch(IllegalArgumentException e)
    {
    System.out.println(e.getMessage());
    }

     System.out.println("Deposit made. Current account balance =  "+ myBalance);
     System.out.print("Enter the amount you would like to withdraw-->");
     try
     {         
     double hi = keyboard.nextDouble();
     withdraw(hi);
    }
    catch(IllegalArgumentException e)
    {
    System.out.println(e.getMessage());
    }    
     System.out.println("Withdrawal made. Current account balance = "+myBalance);
     System.out.println();
     System.out.print("Thank you for using ErrorFreeChecking");









  }
}

i made a simple driver that just calls input

also, if i were to enter a negative # for the 1st question, it dosent produce the error message for that.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

These 2 lines do not make sense

double a = keyboard.nextDouble();             
 a = getBalance();

Input a user value from the console and storing it into variable a
Erasing the just read value from the user calling getBalance()
getBalance() in any case should be called from a Check object

So
Check check = new Check();
a = check.getBalance();
is valid but you are still erasing the value you just read

Please use the [ code] and [ /code] tags when you post code

Edited 5 Years Ago by pbl: n/a

This article has been dead for over six months. Start a new discussion instead.