Reputation: 5
I am writing a program to return the future value based off some input the user gives
import java.util.Scanner; //import utility package, scanner class
import java.lang.Math; //import language package, math class
class InvestmentCalculation
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
//Principle Value Input
System.out.print("Enter principle deposit: ");
int p = s.nextInt ();
//Interest Rate Input
System.out.print("Enter annual interest rate: ");
int r = s.nextInt ();
double fv = p * Math.pow( (1.0 + r/100), 10);
//operation print
System.out.println("Your investment will be worth: " + fv);
}
}
When I run the program, after I type in the rate it gives me the following error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at InvestmentCalculation.main(Addition.java:20)
Upvotes: 0
Views: 515
Reputation: 1605
This is because you are probably entering the rate as a double and the input is thus incorrect.
NOTE: The InputMismatchException
in the JavaDocs reads as follows:
Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
Fix via:
//Interest Rate Input
System.out.print("Enter annual interest rate: ");
double r = s.nextDouble();
Upvotes: 2