• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

InputMismatchException  RSS feed

 
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.
 
Java Cowboy
Posts: 16084
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".
 
Bartender
Posts: 2200
46
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.
 
Marshal
Posts: 61774
193
  • 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!
 
Rancher
Posts: 3757
40
  • 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: 2200
46
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!