Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: using Eclipse and every program gives the errorr with floating numbers

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation using Eclipse and every program gives the errorr with floating numbers

    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;
    }
     
    }
    Last edited by helloworld922; December 30th, 2010 at 11:57 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: using Eclipse and every program gives the errorr with floating numbers

    You're running into localization issues. Depending on your localization settings, Java will either recognize commas as the decimal point or periods as the decimal point, but not both.

    The simplest way to fix this is read in the string and replace all decimal points with commas.

    String tempString = Scanner.nextLine();
    double value = Double.parseDouble(tempString.replace('.', ','));

Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 PM
  2. How to add line numbers in Eclipse
    By Brt93yoda in forum Java JDK & IDE Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 11:30 PM
  3. Eclipse
    By nasi in forum Java IDEs
    Replies: 3
    Last Post: May 6th, 2010, 01:35 PM
  4. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM
  5. Eclipse on Mac
    By Cuju in forum Java IDEs
    Replies: 6
    Last Post: March 19th, 2010, 09:29 PM

Tags for this Thread