Home > Enterprise >  java input mismatch in double value?
java input mismatch in double value?

Time:05-01

so I'm learning java and I've been searching for a solution for this problem for two days I've tried all the decimal separators and tried to set the locale put it didn't work.

code:

output and error

CodePudding user response:

Try adding a call to scanner.nextLine() after your calls to scanner.nextInt() and scanner.nextDouble(). The latter two scanner methods do not eat up the new line character that is produced when you press Enter so my hypothesis is that when you are calling scanner.nextDouble() it's finally eating the \n newline character and causing the InputMismatchException.

TLDR: As a rule of thumb, you should always use scanner.nextLine() after you use scanner.nextInt() or scanner.nextDouble() to ensure that the scanner buffer is clear.

CodePudding user response:

Try using scanner.nextLine() method to read the double value. But the prefferd method is scanner.nextDouble() for double values. If you are going to hit enter and for new line this method will not work so u can use scanner.nextLine(); Hope this will solve your issue.

You can also check out my java tutorials in youtube. https://www.youtube.com/channel/UCY1nXxsoddSnQFJPfjCFuxg/videos

  •  Tags:  
  • java
  • Related