• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not getting "Scanner"  RSS feed

 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is as follows:



If I input the number "5" or "5,0", the program works. For some reason it doesn't work if I write "5.0", despite printing "5.0" itself if I write "5,0". What's up with this?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should be specific when you say your program "doesn't work." If you're getting an error, copy and paste that into your post, between code tags if it's long or if formatting is important. That will give us the opportunity to explain to you what the error means and for you to learn how to fix it yourself next time you see it.
 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is as follows:


Enter a number between 0 and 1:
5.0
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at test.main(test.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1
 
Abdullah Al
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

works fine here
Enter a number between 0 and 1:
5.0
5.0




Jonathan Wallace wrote:The error is as follows:


Enter a number between 0 and 1:
5.0
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at test.main(test.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1
scanner.png
[Thumbnail for scanner.png]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Abdullah. It works fine on your system and not on Jonathan's system because on your system, the default locale uses a dot "." as the decimal point, and on Jonathan's system a comma "," is used.

Abdullah, when you enter 5,0 instead of 5.0, you'll most likely get the same error.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JW, to Jesper's point, is it correct that your system or locale uses the comma as a decimal point, and is that the way you want it to be? From your original post, you seem to be surprised at that behavior. If you would prefer that your locale settings expect the period to be the decimal point, fix your locale settings. If you're not sure how, let us know, and we'll help you figure it out. If you do need our help, let us know what editor/IDE and OS you're using.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!