Results 1 to 4 of 4
  1. #1
    Neera is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Got an error when enter the number in decimal eg 30.23

    I am at the very initial stage of learning Java.I wrote basic program called account.Program is working fine when I enter the number without decimal eg.34,64....But it gives the following error when I enter the nuber with decimal eg.34.87,54.65

    ************************************************** ********
    account1 balance:$50,00
    account2 balance:$0,00
    Please enter the amount you want to deposit in account1 :
    45.63
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at neera.Exercise.Account.AccountTest.main(AccountTes t.java:32)
    ************************************************** ********

    My code is as follows
    ************************************************** ********
    AccountTest.java
    ************************************************** ********
    package neera.Exercise.Account;



    import java.util.Scanner;

    public class AccountTest {

    public static void main(String[] args){

    Account account1 = new Account(50.00);
    Account account2 = new Account(0.00);

    // Display initial balance of each object

    System.out.printf("account1 balance:$%.2f\n", account1.getBalance());
    System.out.printf("account2 balance:$%.2f\n", account2.getBalance());
    // Create Scanner to obtain input from command window
    Scanner input = new Scanner(System.in);
    double depositAmount;

    /**
    * This is for measuring the account after depositing money in the
    * account1
    */

    System.out
    .println("Please enter the amount you want to deposit in account1 :");



    depositAmount = input.nextDouble();
    System.out.printf("\n adding %.2f to account1 Balance", depositAmount);
    account1.credit(depositAmount);
    System.out.printf("Account1 balance:$%.2f\n", account1.getBalance());
    System.out.printf("Account2 balance:$%.2f\n", account2.getBalance());




    /**
    * This is to measuring the account after withdrawing money from
    * account1
    */

    System.out.println("Please enter the amount you want to withdraw:");

    double withdrawamount;
    withdrawamount = input.nextDouble();
    System.out.printf("The amount withdraw is $%.2f\n", withdrawamount);
    account1.debit(withdrawamount);
    System.out.printf("Account1 balance=$%.2f\n", account1.getBalance());
    System.out.printf("Account2 balance=$%.2f\n", account2.getBalance());



    /**
    * This is for measuring the account after depositing the money in
    * account 2
    */

    System.out
    .println("Please enter the amount you want to deposit in account2 :");
    depositAmount = input.nextDouble();
    System.out.printf("adding %.2f to account2 balance", depositAmount);
    account2.credit(depositAmount);
    System.out.printf("Account1 balance:$%.2f\n", account1.getBalance());
    System.out.printf("Account2 balance :$%.2f\n", account2.getBalance());

    /**
    * This is to measuring the amount withdraw from account2
    */
    System.out
    .println("Please enter the Money you want to withdraw from account2:");
    withdrawamount = input.nextDouble();
    System.out.printf("The amount Withdraw from account2 is :$%.2f\n",
    withdrawamount);
    account2.debit(withdrawamount);
    System.out.printf("Account1 Balance is:$%.2f\n", account1.getBalance());
    System.out.printf("Account2 balance is :$%.2\nf", account2.getBalance());


    }




    }
    ************************************************** ********
    Account.java
    ************************************************** ******
    package neera.Exercise.Account;

    public class Account {
    private double balance;

    public Account(double initialBalance) {
    if (initialBalance > 0)
    balance = initialBalance;
    }
    public void credit(double amount){
    balance=balance+amount;
    }
    public void debit(double amount){

    if(balance<amount){
    System.out.println("Debit account exceeded account balance ");
    }else{
    balance=balance-amount;
    }
    }

    public double getBalance() {
    return balance;
    }

    }

  2. #2
    temyong is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    hey Neera. hm next time when your'e posting code be sure to put the code tags just click the # :D .
    o well, I found where your problem is and it is that you forgot to put the f in the last statement of your AccountTest class..

    here's the code.
    Java Code:
    import java.util.Scanner;
    
    public class AccountTest {
    
    		public static void main(String[] args){
    		
    		Account account1 = new Account(50.00);
    		Account account2 = new Account(0.00);
    		
    		// Display initial balance of each object
    		
    		System.out.printf("account1 balance: $%.2f\n", account1.getBalance());
    		System.out.printf("account2 balance: $%.2f\n", account2.getBalance());
    		
    		// Create Scanner to obtain input from command window
    		Scanner input = new Scanner(System.in);
    		
    		
    		/**
    		* This is for measuring the account after depositing money in the
    		* account1
    		*/
    		
    		System.out.println("Please enter the amount you want to deposit in account1 :");
    		double depositAmount;
    		depositAmount = input.nextDouble();
    		
    		System.out.printf("\nadding %.2f to account1 Balance\n", depositAmount);
    		account1.credit(depositAmount);
    		System.out.printf("Account1 balance: $%.2f\n", account1.getBalance());
    		System.out.printf("Account2 balance: $%.2f\n", account2.getBalance());
    		
    		
    		
    		
    		/**
    		* This is to measuring the account after withdrawing money from
    		* account1
    		*/
    		
    		System.out.println("Please enter the amount you want to withdraw:");
    		
    		double withdrawamount;
    		withdrawamount = input.nextDouble();
    		System.out.printf("The amount withdraw is $%.2f\n", withdrawamount);
    		account1.debit(withdrawamount);
    		System.out.printf("Account1 balance=$%.2f\n", account1.getBalance());
    		System.out.printf("Account2 balance=$%.2f\n", account2.getBalance());
    		
    		
    		
    		/**
    		* This is for measuring the account after depositing the money in
    		* account 2
    		*/
    		
    		System.out.println("Please enter the amount you want to deposit in account2 :");
    		depositAmount = input.nextDouble();
    		System.out.printf("adding %.2f to account2 balance\n", depositAmount);
    		account2.credit(depositAmount);
    		System.out.printf("Account1 balance:$%.2f\n", account1.getBalance());
    		System.out.printf("Account2 balance :$%.2f\n", account2.getBalance());
    		
    		/**
    		* This is to measuring the amount withdraw from account2
    		*/
    		System.out.println("Please enter the Money you want to withdraw from account2:");
    		withdrawamount = (double)input.nextDouble();
    		System.out.printf("The amount Withdraw from account2 is : $%.2f\n",	withdrawamount);
    		account2.debit(withdrawamount);
    		System.out.printf("Account1 Balance is: $%.2f\n", account1.getBalance());
    		System.out.printf("Account2 balance is : $%.2f\n", account2.getBalance()); //<-------------------you forgot to put f after %.2
    	}
    }
    Java Code:
    public class Account {
    	private double balance;
    
    	public Account(double initialBalance) {
    	if (initialBalance > 0)
    		balance = initialBalance;
    	}
    	
    	public void credit(double amount){
    		balance=balance+amount;
    		//balance += amount; //or you can try this.
    	}
    	
    	public void debit(double amount){
    		if(balance<amount){
    			System.out.println("Debit account exceeded account balance ");
    		}else{
    			balance = balance-amount;
    		}
    	}
    
    	public double getBalance() {
    		return balance;
    	}
    }
    hope it helps you..:p

  3. #3
    Neera is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for reply,
    I have add the f to my program,but it has still the same error with decimal numbers.

  4. #4
    temyong is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    I have add the f to my program,but it has still the same error with decimal numbers.
    i don't know but it works fine for me..:confused:

Similar Threads

  1. truncate a decimal number ?
    By hacikho in forum New To Java
    Replies: 5
    Last Post: 10-13-2010, 08:58 PM
  2. Converting whole number into decimal
    By jim01 in forum New To Java
    Replies: 2
    Last Post: 09-23-2010, 07:58 PM
  3. Store the decimal number into an variable
    By fataguila in forum New To Java
    Replies: 4
    Last Post: 02-01-2010, 07:22 PM
  4. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 04:57 PM
  5. Converts a binary number to a decimal
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:57 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •