• Post Reply Bookmark Topic Watch Topic
  • New Topic

InputMismatchException  RSS feed

 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
Got another little issue I'm affraid. Trying to calculate a price including a specific intrest.



And here is the method:

public static double calculateInterest(double m, double p) {

return m * p;

Works fine with integers but not with doubles.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you giving the program as input?

You're probably typing in something that cannot be parsed as a double. For example, you are typing in "3,2" instead of "3.2".
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What input do you provide? What is your default locale?
Be careful which decimal separator you use. For some locales it is a dot, and for some it is a comma.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the problem. I can see nothing wrong with that code. You run it, it tells you to enter the numbers
Set interest and price: 1234567 1.23456
… and the method returns the result. What is the actual Exception? The only way you can get that Exception is by entering something which is not a decimal number
Set interest and price: $1234567 1.23456%
How do you get that method to work with integers and not with floating‑point numbers?
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:What exactly are you giving the program as input?

You're probably typing in something that cannot be parsed as a double. For example, you are typing in "3,2" instead of "3.2".


Funny thing is I acually was typing 3.2 with the correct (.) and not the (,). Then I decided to try the "wrong" (,) intstead and it worked! Hmmm?


edit:

Set interest and price:
1,25
100
125.0

WORKS!
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:
Funny thing is I acually was typing 3.2 with the correct (.) and not the (,). Then I decided to try the "wrong" (,) intstead and it worked! Hmmm?



What is your locale set to?
That's what defines the correct format for a decimal on your machine.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you want a different locale, you can use Scanner.useLocale() method.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!