0

I have this code here:

import java.util.Scanner;

public class Loan {
public static void main(String[] args) {
  Scanner input = new Scanner(System.in);

  System.out.print("Enter annual interest rate, e.g., 7.35%: ");
  double annualInterestRate = input.nextDouble();

  double monthlyInterestRate = annualInterestRate / 1200;

  System.out.print("Enter number of years as an integer, e.g., 5: ");
  int numberOfYears = input.nextInt();

  System.out.println("Enter loan amount, e.g., 120000.95:" );
  double loanAmount = input.nextDouble();

  double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow
          (1 + monthlyInterestRate, numberOfYears * 12));

  double totalPayment = monthlyPayment * numberOfYears * 12;

  System.out.println("The monthly payment is $" + 
    (int)(monthlyPayment * 100) / 100.0);
  System.out.println("The total payment is $" + 
    (int)(totalPayment * 100) /100.0);

   }
}

The annualInterestRate is a double value but when i try to enter a decimal like 5.4, i get an error. Using whole numbers works perfectly fine. Anything wrong with the code? Thanks:)

Son.Dre
  • 113
  • 2
  • 9
  • 1
    Please post more information about the error. Is an exception thrown? If so, post the stack trace. – StvnBrkdll Dec 08 '16 at 13:07
  • At a glance, it looks to be because you're trying to convert the monthlyPayment double to an int. When the double value isn't a whole number, Java won't know what to do with it. Can't say for certain without details of the error. – Reisclef Dec 08 '16 at 13:07
  • 1
    have you tried using `5,4` as input? Maybe your locale is off. – luk2302 Dec 08 '16 at 13:10
  • @mangotang Enter annual interest rate, e.g., 7.35%: 5.4 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 Loan.main(Loan.java:7) – Son.Dre Dec 08 '16 at 13:10
  • 1
    Consider the Locale ....7.35 is not the same as 7,35 when you have different locale set – ΦXocę 웃 Пepeúpa ツ Dec 08 '16 at 13:13
  • 1
    Doesn't seem to error for me when I enter 5.4 as the interest rate. – Reisclef Dec 08 '16 at 13:13
  • I dont really know much about it, but I don't think the problem is that i cast the double to int because it is in the output section. Could it be something wrong with my settings in eclipse? – Son.Dre Dec 08 '16 at 13:13
  • No errors for me, my input: 9.9 2 120.98 – StvnBrkdll Dec 08 '16 at 13:15
  • 1
    But did you tried with `7,35` as _luk2302_ and _ΦXocę 웃 Пepeúpa ツ_ said. This is generaly the reason why a `nextDouble()` failed. Because depending on your system (and the locale set), the decimal is `,` or `.`. Use `System.out.println(Locale.getDefault().getDisplayName());` to see the Locale you use – AxelH Dec 08 '16 at 13:17
  • Weird, I tried using a comma instead of a period and it worked! Thanks for your help :) – Son.Dre Dec 08 '16 at 13:22
  • Yea maybe not, but the book i was reading used period. Im from Norway – Son.Dre Dec 08 '16 at 13:28
  • Check this [list](https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html), you will see that the locale for Norway is like `4.294.967.295,000`. See my answer to change that. – AxelH Dec 08 '16 at 13:30
  • Ok, good to know thank you. – Son.Dre Dec 08 '16 at 13:33

1 Answers1

0

Your locale (Norway) use , as decimal delimiter as it is specified in the list of Oracle looks like :

4.294.967.295,000

As the doc sais :

A scanner's initial locale is the value returned by the Locale.getDefault()

But you can change the Scanner locale with useLocale(Locale) like this :

Scanner sc = new Scanner(ystem.in);    
sc.useLocale(Locale.ENGLISH);

As said by the doc

It may be changed via the useLocale(java.util.Locale) method

This will accept a . as delimiter.

The full documentation is here

AxelH
  • 14,325
  • 2
  • 25
  • 55