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

Using try-catch to handle input exceptions in Java

I have been working on this for hours and I just can't make sense of how to implement a try-catch in these two do while loops to catch if user enters a non-int. I know there are posts on this, but I can't seem to get it. I greatly appreciate any…
Chris
  • 13
  • 1
  • 6
0
votes
2 answers

How to filter Scanner input errors with try/catch/finally in my programm?

I wrote this little guessing game where you have to guess of cause a random number. I finished this programm but the last thing I would like to to do is: When the user should input a number and its and InputMissmatching error or other errors, the…
0
votes
1 answer

InputMismatchException issue with For loops and If Else Statement

I'm new to Java. I would really appreciate it if you could help me figure this out. I'm trying to make a program to read user inputs (integers) and store them into an array and print them out afterwards. I use a variable called currentSize to keep…
0
votes
1 answer

trying to understand how to handle InputMismatchException and keep the program running

I'm writing a simple game that asks a user to type in an integer. If they type in anything else I want to tell them it was invalid and have them try again. I am using eclipse and it seems like no matter what I try when the user(me) puts in an…
0
votes
0 answers

Java: Infinite loopback error using InputMismatchException

So I am reworking my java beginner "Divide" program, where you have to divide two integers inputted by user. My problem is when inputting an alphabet or symbol, i get an infinite loop. The catch seems to work fine (output reads error) but it's a…
0
votes
0 answers

Q: Error: Exception in thread "main" java.util.InputMismatchException

I am writing a program to solve the all-pairs shortest paths problem using the Floyd-Warshall algorithm. I am given a test file in txt format in which the first int is the number of vertices, the second int is the value of infinity and the rest is a…
0
votes
0 answers

Adding +1 to an Int next.Int value in Java + InputMisMatch error

I keep getting an error message saying I have an input mismatch. I just can't seem to figure out where the mismatch is happening. Does somebody see what I'm doing wrong? The other question I have is I want to add +1 to the result.x for each item…
Zsa_Sza
  • 15
  • 1
  • 6
0
votes
1 answer

One Loop works perfectly and the next identical Loop fails

The first loop in the second method works perfectly. when the user inputs characters instead of numbers the InputMismatchException is caught and the user is prompted to re-enter numbers only in order to continue. The second loop fails: If characters…
0
votes
1 answer

InputMismatchException error in java code. Please give some advice

For some reason I keep getting the inputmistmatch exception on line 44 (int fileAge = inputFile.nextInt();) I have tried isolating that part of the code and using it with a manually created file and it still gives the same error. I have tried using…
roxas0010
  • 3
  • 2
0
votes
2 answers

InputMismatchException when working with java.util.Scanner in Java

I'm trying to create a sort of inventory storage program where users can enter, remove, and search for items and prices. However when entering values, I get an InputMismatchException. Here is the WIP code I have so far: String item; double…
NB111
  • 35
  • 4
0
votes
1 answer

Making a power with an double base and an integer exponent without any math. library in Java

I need to make a power of a double base with an integer exponent without using any math. library but I get the error: package potencia; import java.util.Scanner; public class Potencia { int n=0; double b=0,p=0, potencia=1; Scanner entrada=…
0
votes
0 answers

InputMismatchException when using nextInt

I am reading a text file to get the first Int, which is used to set the dimensions of a grid, and then it should read a series of rows of ints to populate that grid. I get the InputMismatchException on the thirdline of numbers. How do I get it to…
CFuentes
  • 15
  • 5
0
votes
1 answer

What's the best way to read input in Java, especially for consecutive strings? I don't know when to use next() and nextLine()

I'm getting this error in a really simple code and I am not able to figure it out. Also, please suggest what is the best way to read two consecutive strings, like in this case. I face InputMismatchException and I think that it could be the cause.…
0
votes
0 answers

How to read a double from a file using Scanner without InputMismatchException

I am doing a simple task. I am assigning strings, ints, and a double to variables. The variables are being assigned through a scanner reading a text file. The scanner cannot read the double. It throws a input mismatch exception. I have researched…
0
votes
2 answers

How to read input in an online test in Java - InputMismatchException

I got an InputMismatchException in Java when I tried to read the following input. Input 1 // Integer aaa // String IND // String 1.2 // Double My code was like this: Scanner in = new Scanner(System.in); int n =…