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
0
votes
1 answer

How to input 1~2 instead of 1 2. InputMismatchException

So I was solving a problem on Techgig which goes like this : There I have to print the sequence of Fibonacci numbers till 10 places in array and the first two inputs were entered by the user. My code goes like: import java.io.*; import…
0
votes
2 answers

Why does this code keep on looping if there's an InputMismatchException?

Why does this code keep on looping if there's an InputMismatchException? When you input a zero, the catch works, but if you enter a string, the loop becomes infinite. import java.util.*; public class TryCatch { public static void…
gendave
  • 3
  • 3
0
votes
2 answers

inputMismatchException java

( ANSWER: in the switch case I forgot my break statements and I guess that was throwing the Scanner off! ) I'm writing a program to read a file that contains three kinds of shapes, followed by their corresponding dimension and write them into an…
Evan Jones
  • 43
  • 7
0
votes
3 answers

Issue with Reading two Int values from a file

I have file with the below data A,8,43 B,7,42, C,9,34 I am using the below code to read the data Scanner input = new Scanner(new File("D:\\test.txt")); input.useDelimiter(",|\n"); while(input.hasNext()) { String name = input.next(); int…
0
votes
2 answers

Getting an "Exception in thread "main" java.util.InputMismatchException" error inside a do While Loop

import java.util.Random; import java.util.Scanner; public class Jogo { Scanner scanner = new Scanner(System.in); int tipoJogo; int turno; Barco[] jfA,jfB; Random random = new Random(); public Jogo(Barco[]fA, Barco[]fB, int tamanhomax, int tj) { …
0
votes
3 answers

Can't use scanner for string and integer at the same time

I'm working on this code which takes an integer as a test case then takes a string and an integer for each test case But I keep getting this exception: Exception in thread "main" java.util.InputMismatchException at…
0
votes
0 answers

Constant loop when InputMismatchExpection has been caught instead of retrying/scanning keyboard input again

Whenever an Input Mismatch is caught there is an infinite loop (example entering ];), instead of trying the "option = scanner.nextInt();" again. However typing a normal invalid input like "7" works fine. I don't want to use a "break;" because I want…
0
votes
1 answer

Scanner Mismatch Exception when reading text file

My scanner is reading from a text file with the use of a delimiter. However when I run the program the only line that gets printed out is the first line of data then I get thrown an input mismatch exception. I believe the error is that it doesn't…
Luke G
  • 25
  • 1
  • 1
  • 3
0
votes
1 answer

Java InputMismatchException Error

I've been given a task to make some conversions from ft and in to cm. I've gotten most of this down and the conversions do work. I also want to include the statement of A negative number... or A non-digit... when I type a string, for example, or a…
adhamncheese
  • 111
  • 9
0
votes
3 answers

System input in java doesn't work correctly

I'm trying to make a program that takes Ints and give me sum and I want to make it using regex. Input contains numbers, symbols and letters. For example when I write: java GiveMeSum 4 2 1 -5 or java GiveMeSum 4k "2 1 !-5 program should write 2 But…
0
votes
1 answer

Why am i getting this error?(Double Array Method)

I am getting the error "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.nextInt(Scanner.java:2160) at …
user6267046
0
votes
1 answer

Cannot find reason for InputMismatchException

I'm having an issue with Scanner in that it seems to be taking the input value type and forcing the next time the user inputs a value to be of the same type. I cannot find any reason why this code is not working and gives me an…
0
votes
1 answer

InputMismatchException in thread "main"

First of all i am compete noob in Java. I tried to get the user input. Eclipse shows this error whenever i try to type double variable: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at…
user7332667
0
votes
0 answers

persistent InputMismatchException errors

In this code, I create a scanner to read a file full of ASCII values, which are ints. I put them in a 2d array of chars using nested loops after I typecast them to type 'char', and then proceed to print the resulting arrays in a separate method. the…
0
votes
2 answers

Exception Handling for no user input in Java

I am trying to get my program to exception handle for if the user inputs nothing so they will get an error message of "Error, enter a dollar amount greater than 0" or "Error, Enter a 1, 2 or 3". As of now, the program does nothing if the user just…
ecooper10
  • 107
  • 1
  • 3
  • 13