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
1
vote
3 answers

Java InputMismatchException unknown source

I am having trouble finding the source of my error. All I am doing is reading text from a file public static void main(String[] args) throws Exception { int T; Scanner sc = new Scanner(new FileInputStream("problem3.txt")); T =…
wO_o
  • 181
  • 3
  • 13
1
vote
2 answers

Input mismatch error

So, I have this little program here and I've been getting the input mismatch error. I'll post the error message below the code. To be honest, I have 0 clue why the error appears. I've checked that all the required variables have correct type…
George Waat
  • 167
  • 2
  • 8
1
vote
5 answers

Why is this do while loop running infinitely?

Trying to write a program in which the user inputs their expenses for the week. The trouble comes in the fact that I want the program to re-ask the user if unacceptable input is entered. I figured out how to detect negative values, but when trying…
Clarisa
  • 125
  • 2
  • 12
1
vote
1 answer

Why my code runs to infinite loop when I enter a non-integer input?

Why I'm having an infinite loop when I enter a non-digit token? public class ExceptionHandling { static int i; /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code…
Arjun Chaudhary
  • 2,373
  • 2
  • 19
  • 36
1
vote
1 answer

Why get i InputMismatchException when i use scanner sc.nextDouble()

I'd like to read data from a txt file, but I get InputMismatchException when I call nextDouble() method. Even though I am using the useLocale method, but it doesn't work. The txt file first line is: 1;forname;1.9 public class SimpleFileReader { …
Bbeni
  • 75
  • 8
1
vote
1 answer

Exception Handling in a while loop

The problem I am having with this code is the following: the variable number can't be a String so I tried to use a try, catch (InputMissmatchException) statement to take care of the issue. However, when in goes in the loop and someone enters a…
1
vote
1 answer

Executing a try/catch within a while loop

I am trying to execute a try catch block within a while loop. When I ask the user to enter a number (which should be a double) I use a try catch to catch any input mismatch exceptions. I nested this inside a while loop so that if any exceptions are…
1
vote
4 answers

Catch/try not working for input mismatch exception with scanner?

Alright so I'm taking a CS security class that has some basic java programing in it and our first assignment is to play with BigInteger. However, we also have to "bullet proof" our program. And while my method isn't the most ideal, it works EXCEPT…
1
vote
1 answer

try catch on looping (differnet between InputMismatchException and ArrayIndexOutOfBoundsException)

I have this code package example; import java.util.InputMismatchException; import java.util.Scanner; public class Example { public static void main(String[] args) { Scanner input = new Scanner(System.in); int rep; int[] arraya = new…
1
vote
1 answer

Java InputMismatchException?

I'm try to do an exception on my codes if ever the user puts a string instead of an integer. My codes will swap the position of the largest index to the smallest index. Can you try to rectify this with me? import java.util.Scanner; import…
1
vote
2 answers

Reading from an Input File to an Integer Array

I'm currently coding a project in Java that will take an input file and read it into several parallel arrays. There are several restrictions--we can't use array lists, files must be read using Scanner. After reading it into the arrays, there are a…
user4285389
1
vote
1 answer

InputMismatchException on Scanner.nextInt

I'm trying to use a Scanner to draw a rectangle in JFrame, but get these errors: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at…
joshbenner851
  • 111
  • 1
  • 12
1
vote
4 answers

exceptions and infinite loops

// Precondition: number provided is a positive integer // Postcondition: returns a integer of length 4 public static int validateNumber(int num, Scanner scan) { int number = num; while(number < 1000 || number > 9999) { …
Alan
  • 153
  • 1
  • 4
  • 11
1
vote
3 answers

java.util.InputMismatchException by reading a double

I got the an java.util.InputMismatchException when I try to read 0.6 here is a part of the code. As you can see i try to reimplement a SkipList for a exercise sheet. public static void main(String[] args) { Scanner scan = new…
Sven Bamberger
  • 899
  • 2
  • 10
  • 21
0
votes
0 answers

I keep having InputMismatchException error every time I type the name of the person in the Scanner?

what is the problem with my code and why do I have the InputMismatchException error every time I type the name in the console ? Scanner sc = new Scanner(System.in); System.out.println("How many People are there ? (employee and students…
Vincent
  • 1
  • 1