Questions tagged [inputmismatchexception]

Use this tag on questions regarding input mismatch exceptions that occur when using the `Scanner` object.

The InputMismathException is a Java I/O exception that is thrown when the Scanner object encounters an input which it does not expect. An example would be expecting a double, but receiving a String token instead. This is a runtime exeception that inherits from NoSuchElementException.

258 questions
33
votes
2 answers

Scanner double value - InputMismatchException

I tried use scanner at easiest way: Code: double gas, efficiency, distance, cost; Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of gallons of gas in the tank: "); gas = scanner.nextDouble(); System.out.print("Enter…
catch23
  • 17,519
  • 42
  • 144
  • 217
10
votes
5 answers

Why am I getting InputMismatchException?

So far I have this: public double checkValueWithin(int min, int max) { double num; Scanner reader = new Scanner(System.in); num = reader.nextDouble(); while (num < min || num > max) { …
Trần Trung
  • 171
  • 1
  • 2
  • 9
7
votes
2 answers

Exception in thread "main" java.util.InputMismatchException

I need help with one exercise in java, I'm stuck on this error for 2 hours maybe. Any help would be great. Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at…
tonyhlav
  • 393
  • 3
  • 6
  • 7
6
votes
1 answer

Java: Try-Catch statement within a Do-While loop

I am trying to execute a bit of code that scans for a user input value. This action is contained in a custom method I wrote, named getTriangleDim(); the method reads in the users int value, makes sure it is within a certain range, and then returns…
user2993456
6
votes
3 answers

Asking for input after catching an exception

I want the user to enter a number which is scanned by the following code: scanner.nextInt(); If a user enters a string instead, the program throws InputMismatchException, which is obvious. I want to catch the exception in such a way that the…
5
votes
8 answers

Java, try-catch with Scanner

I am creating a small algorithm and this is a part of it. If the user enters non integer values, I want to output a message and let the user enter a number again: boolean wenttocatch; do { try { wenttocatch = false; …
4
votes
5 answers

Try-Catch inside While Loop

The code below asks the user how many racers he/she would like. while (true) { // loops forever until break try { // checks code for exceptions System.out.println("How many racers should" + " participate in the race?"); …
Rhendz
  • 101
  • 1
  • 1
  • 9
4
votes
5 answers

Looping do...while uncertainty

In the catch block, I'm trying to correct for user bad-input issues. When testing it, if I use the "break" keyword, it doesn't jump to the initial question. If I use "continue", it loops infinitely. "Sc.next();" doesn't seem to resolve it either.…
3
votes
4 answers

Why does this throw an error/not run at all?

I'm running this in VS code and compiling and running through terminal with the proper JDK installed but nothing seems to run it just goes blank and then I type "clear" and a mismatch exception is thrown. Can someone explain why? Thank you :) import…
3
votes
1 answer

handle Exceptions casued by input scanner

I'm trying to do encode/decode program and I'm encountering all kinds of Exceptions here! problems that is popping up caused by multiple/single scanner/s: InputMismatchException | NumberFormatException (ATTEMPT 2) NoSuchElementException (ATTEMPT…
3
votes
2 answers

(Java) Scanner not reading in nextDouble() from file

I'm new to Java and I'm trying to use a scanner to read in some data from a text file. The scanner reads in the string and integer data members fine, but when it reaches a double it throws an InputMismatchException. The format of the text file…
3
votes
0 answers

Java scanner throws InputMismatchException for trying to read nextDouble()

im trying to read a integer and two doubles from a text file. I wrote a shorter example demo of my problem. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Test { public static void…
koin
  • 203
  • 2
  • 12
3
votes
1 answer

Input Mismatch Exception?

I'm still fairly new to java. I created this program for class and it's giving me an error that I have never gotten before. If someone could help that would be great. Thank you! import java.util.Scanner; import java.io.*; public class grades { …
3
votes
1 answer

InputMismatchException:null

int EID, ESSN, EM, FSSN, FM; String EN, EEN, FN; Scanner Ein = new Scanner(employer); Scanner Fin = new Scanner(filers); Ein.useDelimiter(", *"); Fin.useDelimiter(", *"); ArrayList EmployIn = new…
Ethan
  • 31
  • 2
3
votes
2 answers

Asking user to enter the input again after he gives a wrong value for the Input. InputMismatchException?

I have created the following class for Inputting a user's age and then displaying appropriate info in the console. On running this program , the console asks "Please Enter your Age : " If the user enters an Integer for eg: 25 , the executed class…
Anshuman Tripathy
  • 113
  • 1
  • 3
  • 12
1
2 3
17 18