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

Scanner using delimiter but not recognizing the integer after it

I can't seem to find the issue in my code and was wondering if you wonderful people on here would be willing to help me out. My professor is requiring us to create a 2-D array from information pulled from a file she provided us. Using the Scanner…
0
votes
1 answer

Why am I getting inputMismatchException even when using hasNextInt() method?

This program asks the user to input their student id number which must be an integer between 0-999999. The testID method that has been commented out uses a do while loop with an inner while loop that ensures only integer values are input by the…
0
votes
1 answer

InputMismatchException crashes program after being caught

Introduction I am having a problem catching an InputMismatchException. I have read all of the threads on this site that appear on the first page when I search for InputMismatchException already. I found the answer to one of my questions while…
percepmoid
  • 45
  • 5
0
votes
2 answers

Trying to prompt the user to re-enter from the catch block, but the catch block terminates?

I am trying to write a program to ask a user to enter their age and prompt them to re-enter if they enter an improper value (such as a negative number, older than 120, an age with special characters or letters, an out of range number etc...) I tried…
taralee98
  • 127
  • 1
  • 12
0
votes
3 answers

Java Runtime Error InputMismatchException in program

When I run this code I get this code in coderunner (an app that submits code for schoolwork) Scanner scan = new Scanner(System.in); double maxn = -90; double maxs = 90; double maxe = 180; double maxw = -180; double lat = 0; …
0
votes
1 answer

Scanner with Delimiter as new line - java.util.InputMismatchException in Eclipse

I have a small program to scan an Integer from console, also I would like to use new line character as delimiter. import java.util.Scanner; public class TestScanner { public static void main(String[] args) { Scanner scanner = new…
0
votes
2 answers

Why am I not getting any output? And there is an InputMismatchException

I am trying to solve a practice problem on hackerrank. But, my output is not displaying and the program throws an InputMismatchException can someone please help me out? Input Format The first line contains and integer n, the size of variable…
sidzama
  • 33
  • 6
0
votes
2 answers

scanner error for alphabets

hi In my program if I enter number in the field it should display as valid value and if I enter alphabets or special characters it should be displayed as invalid value ,but when i run the following code it displays error if I enter words as input…
D V
  • 211
  • 6
  • 13
0
votes
0 answers

Java InputMismatchException when reading integers from text file into matrix

I'm reading in numbers to fill an adjacency matrix. The numbers are read in from a file and the format can be seen below. 0 1 0 0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 0 0 1…
user7939273
0
votes
1 answer

Why is this Scanner assigning null to a variable?

For a college assessment I'm having to use a Scanner called sc with a class-level scope, and the entirety of the program has to be contained in a single class. The main method calls a menu() method, which uses the Scanner and a for loop to call one…
0
votes
1 answer

Java- handle an inputMisMatch exceptions

I am a new programmer and I am very inexperienced with exception handling as I am a student. I am writing a program that lets a users schedule appointments from the times of 1 - 6 o'clock. If the user enters a value anything other than an integer I…
0
votes
2 answers

(Java) inputmismatchexception when scanning a file

I get java.util.inputmismatchexception when using nextDouble() on a file containing only doubles. Here is the relevant section of code: public static double[] getGravity() throws IOException { Scanner file = new Scanner("SurfaceGravity.txt"); …
0
votes
0 answers

I'm getting an InputMismatchException message when uploading a text file?

I have a method named readBabyNameData public void readBabyNameData(String filename){ try{ // open the text file and use a Scanner to read the text FileInputStream fileByteStream = new FileInputStream(filename); Scanner…
0
votes
0 answers

Input Mismatch with Scanner

As title states, have a bit of an issue reading things correctly. The catch here is that I am reading only a bit of the file outside of a while loop, and the rest inside. I think that works fine because a piece of the file is read into every…
Kraw24
  • 143
  • 1
  • 7
0
votes
1 answer

InputMisMatch Exception Error

I am trying to code this program that asks the user for the value of x1, and to create an exception if the user inputs something other than an integer. However, when I use the InputMismatchException, I keep getting an error saying that it can not be…
Celina
  • 3
  • 3