0

I'm suppose to enter 2 numbers, one int that is the amount to withdraw and one double which is the balance (with a space between them). Since every withdraw charges a fee of 0.5, balance must be a double. And thats what must be printed. I get error at nextDouble, why? I have just 1 month coding, I thought this was going to be a piece of cake, I think BASIC syntax ruined me 30 years ago :(

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
    //init variables
    int amount;
    double balance;
    //insert amount and balance
    Scanner input = new Scanner (System.in);
    amount = input.nextInt();
    balance = input.nextDouble();
    //reduce amount+fee from balance
    balance=balance-(amount + 0.50);
    //print new balance
    System.out.print(balance);
    input.close();
}
}
Nooblhu
  • 552
  • 15
  • 33
  • What's the input you are giving? And also what's the exact exception you are getting. – Codebender Apr 28 '16 at 10:58
  • 1
    The input was with "."(dot) decimals but my IDE was set to ","(comma). I'm old enough to remember when we changed to commas in math in my country but I still use dots. Changed IDE config to solve. – Nooblhu May 13 '16 at 20:41

2 Answers2

3

It is dependant on Locale, try to use comma instead of a dot or vice versa.

Ex: 1,5 instead of 1.5

Y.E.
  • 902
  • 7
  • 14
  • Yes, I tried to use to locale but then I had to convert to string, which i didnt wanted so instead I changed my Eclipse default setup to english US to use "." for decimals without problems. [How to do that](https://stackoverflow.com/questions/4947484/how-to-set-eclipse-console-locale-language) – Nooblhu Apr 29 '16 at 06:05
0

You can check, if there is some int or double to read. And you have to use , or . depending on the country, you are. If you need it country independent, read it as string and parse then (see below)

A solotion would be to read the line as a string and parse it then to int and double.

Checking if double is available:

input.hasNextDouble();

Read as String:

String line = input.nextLine();
String[] sl = line.split(" ");
amount = Integer.parseInt(sl[0]);
balance = Double.parseDouble(sl[1]); //solve the problem with . and ,

You also could check if there are enough inputs.

Jarlik Stepsto
  • 1,667
  • 13
  • 18