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
************************************************** ********
Code 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
************************************************** ******
Code 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;
}
}
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.
Code Java:
String tempString = Scanner.nextLine();
double value = Double.parseDouble(tempString.replace('.', ','));