This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 2212
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: 62863
203
  • 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: 3887
45
  • 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: 2212
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!