4 Replies - 318 Views - Last Post: 05 October 2013 - 05:48 PM Rate Topic: -----

#1 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 25-October 08

Error on comparison values: noob

Posted 05 October 2013 - 02:44 PM

Team,
I modified a code a requested to display appropriate message to do a withdrawal from an account(s) and display the balance. it works ok but i seem to be getting a run time error when ever i try to do a withdrawal from account2 of the full amount or an amount grater than the available balance. cam you tell me why?


public class Account
{   
   private double balance; // instance variable that stores the balance
   

   // constructor  
   public Account( double initialBalance )
   {
      // validate that initialBalance is greater than 0.0; 
      // if it is not, balance is initialized to the default value 0.0
      if ( initialBalance > 0.0 ) 
         balance = initialBalance; 
   } // end Account constructor
   
   public void debit(double amount)
   {
       if(amount <=  balance)
           balance = balance - amount;
       else
       {
          System.out.println("Debit amount exceeds account balance");
       }
   }

   // credit (add) an amount to the account
   public void credit( double amount )
   {      
      balance = balance + amount; // add amount to balance 
   } // end method credit

   // return the account balance
   public double getBalance()
   {
      return balance; // gives the value of balance to the calling method
   } // end method getBalance
} // end class Account



import java.util.Scanner;

public class AccountTest
{
   // main method begins execution of Java application
   public static void main( String[] args ) 
   {
      Account account1 = new Account( 50.00 ); // create Account object
      Account account2 = new Account( -7.53 ); // create Account object

      // display initial balance of each object
      System.out.printf( "account1 balance: $%.2f\n", 
         account1.getBalance() );
      System.out.printf( "account2 balance: $%.2f\n\n", 
         account2.getBalance() );
      
      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );
      double depositAmount; // deposit amount read from user
      System.out.println( "Enter deposit amount for account1:$" ); // prompt
      depositAmount = input.nextDouble(); // obtain user input
      System.out.printf( "\nadding %.2f to account1 balance\n\n", 
         depositAmount );
      account1.credit( depositAmount ); // add to account1 balance

      // display balances
      System.out.printf( "account1 balance: $%.2f\n", 
         account1.getBalance() );
      System.out.printf( "account2 balance: $%.2f\n\n", 
         account2.getBalance() );

      System.out.print( "Enter deposit amount for account2:$" ); // prompt
      depositAmount = input.nextDouble(); // obtain user input
      System.out.printf( "\nadding %.2f to account2 balance\n\n", 
         depositAmount );
      account2.credit( depositAmount ); // add to account2 balance

      // display balances
      System.out.printf( "account1 balance: $%.2f\n", 
         account1.getBalance() );
      System.out.printf( "account2 balance: $%.2f\n", 
         account2.getBalance() );
      Scanner input1 = new Scanner(System.in);
      int amount;
     
      System.out.println("Please Enter Withdrawal Amount From Account1:$");
        amount = input1.nextInt();
      System.out.printf("\nwithdrawing $%d from accont1, with the balance of %.2f\n",amount,account1.getBalance());
        account1.debit(amount);
      System.out.printf("Balance remaining in Account1 is $%.2f\n",account1.getBalance());
     
      System.out.println("Please Enter Withdrawal Amount From Account2:$");
        amount = input1.nextInt();
      System.out.printf("Withdrawing $%d from Account2 with the balance of $%.2f\n", amount);
        account2.debit(amount);
      System.out.printf("Balance remaining in Account2 is $%.2f\n",account2.getBalance());
                       
   }
   } // end main



Is This A Good Question/Topic? 0
  • +

Replies To: Error on comparison values: noob

#2 ChrisNt  Icon User is online

  • cute bug

Reputation: 212
  • View blog
  • Posts: 647
  • Joined: 31-July 13

Re: Error on comparison values: noob

Posted 05 October 2013 - 02:52 PM

Amount is int you try to print it as two decimal places
System.out.printf("Withdrawing $%d from Account2 with the balance of $%.2f\n", amount);

**
time to sleep

This post has been edited by ChrisNt: 05 October 2013 - 03:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,728
  • Joined: 12-December 12

Re: Error on comparison values: noob

Posted 05 October 2013 - 03:08 PM

Please always post the error message(s) you receive.

The error message in NetBeans is:

Quote

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
at java.util.Formatter.format(Formatter.java:2487)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at javaapplication1.Test.main(Test.java:55)
Withdrawing $10 from Account2 with the balance of $Java Result: 1

This is quite clear and refers to this line:

System.out.printf("Withdrawing $%d from Account2 with the balance of $%.2f\n", amount);

You've specified two format-arguments but you've only supplied one (amount).

This post has been edited by andrewsw: 05 October 2013 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 25-October 08

Re: Error on comparison values: noob

Posted 05 October 2013 - 03:35 PM

View PostChrisNt, on 05 October 2013 - 03:52 PM, said:

Amount is int you try to print it as two decimal places
System.out.printf("Withdrawing $%d from Account2 with the balance of $%.2f\n", amount);

**
time to sleep


thanks alot but i just figured it out as well. i was missing a ref var. i guess thats what happens when you code tired. thanks again.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error on comparison values: noob

Posted 05 October 2013 - 05:48 PM

View Postwilliamscel, on 05 October 2013 - 05:44 PM, said:

i seem to be getting a run time error when ever i try to do a withdrawal from account2 of the full amount or an amount grater than the available balance.

You seem ? You are not sure ?
Don't you think it would help us to help you if you actually post that error that you seem to see ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1