• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 2217
47
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: 64089
215
  • 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!
 
Master Rancher
Posts: 4017
47
  • 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: 2217
47
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.
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!