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
3
votes
4 answers

Code to read from a file and then putting it into a constructor won't work

I'm a college student and I have a project in java and I'm trying to read from files and putting them into a constructor. The file i'm trying to read from is in this form: 2 Sciense [mr ali hassan 14/4/1993 ] Ali Hhassan 13234 12/3/1998 123…
3
votes
5 answers

Eclipse asks for a return statement though I have one

protected int readInt(String prompt) { try { System.out.print(prompt); int i = keyboard.nextInt(); keyboard.nextLine(); return i; } catch (java.util.InputMismatchException e) { …
3
votes
2 answers

InputMismatchException with Scanner

I have the following Java code: Scanner input = new Scanner(System.in); try { System.out.println("Enter your name>>"); String name = input.next(); System.out.println("Enter your year of birth>>"); int age =…
2
votes
2 answers

Scanner#nextDouble fails to parse 5.2 (InputMismatchException)

Problem I was messing around with Java today, and bumped into something that I can't fix by myself. I'm trying to get a double value from scanner but always get this error: Exception in thread "main" java.util.InputMismatchException at…
2
votes
2 answers

Why string inputs as doubles are outputting this answer

I'm very new and I'm just curious to know why this code outputs 201.0 when entering 2E2 as the value of the double. public static void main(String[] args) { double r = 2E2; try{ } catch(InputMismatchException e) { r=-1.0; …
2
votes
3 answers

How do I fix the InputMismatchException when doing the nextInt function in java

I am trying to get a sudoku verifier working by reading in txt file with comma-seperated digits. My code reads the first line into the array then when going to the next line throws an InputMismatchException error. I've tried looking things up but…
user10091649
2
votes
1 answer

Scanner.nextInt() throwing InputMismatchException when reading an integer

I'm having a problem with my code that seems like it should be running perfectly. public void inputFile() { Scanner reader = null; try { reader = new Scanner( new File ("parts.dat")); reader.useDelimiter("\\,|\\n"); …
2
votes
2 answers

InputMismatchException when using Sacnner nextLine for String

This is my code import java.io.*; import java.util.*; class student { String name; int age; float cgpa; } public class getdata { public static void main(String args[]) throws IOException { Scanner in=new…
Nn Karthik
  • 122
  • 3
  • 13
2
votes
5 answers

InputMismatchException for String input into integer field

I am having trouble with entering non-integers into an integer field. I am only taking precautions so that if another person uses/works on my program they don't get this InputMismatchException. When I enter a non-digit character into the input…
Yiuri
  • 31
  • 1
  • 4
2
votes
1 answer

Trouble with for loop continuing after catching an exception

Hi I am semi new at java and cant figure this one out. After catching an exception I want a for loop to continue and keep reading for new integers. This was a challenge put online that wants you to take 5 (this telling how many inputs it should…
JT attack
  • 87
  • 1
  • 1
  • 8
2
votes
3 answers

Why am I getting an input Invalid Infinite Loop?

Why does this cause me to get stuck in an endless loop when the initial choice is invalid? while (true) { System.out.print("Choice:\t"); try { int choice = scanner.nextInt(); break; } catch (InputMismatchException e) { …
2
votes
1 answer

Reading a file with a read method using Scanner (InputMismatchException)

I'm new to java and I have a problem with reading a file using the scanner class. My objective is to read the following .txt file: 3 Emmalaan 23 3051JC Rotterdam 7 rooms price 300000 Javastraat 88 4078KB Eindhoven 3 rooms price 50000…
Kien
  • 23
  • 3
2
votes
2 answers

Try/Catch inside While in static main method

I don't understand the logic to this. If I run this code and enter a non-int such as the letter f, I get stuck in an infinite loop outputting the two println's and I am not given another chance to input an int to the scanner...it just keeps spitting…
Justin
  • 164
  • 1
  • 14
2
votes
2 answers

how do number format and input mismatch exceptions differ

The following code throws a NumberFormatException when a character value is passed in Integer class constructor instead of an integer value class Wrap { public static void main(String...args) { Integer j=new Integer("s"); …
2
votes
4 answers

java.util.InputMismatchException Output Error

I'm currently trying to read a file from my HDD. The file name is "Sample.txt", below is my code. I'm able to get it to compile and run, but receive this error: Exception in thread "main" java.util.InputMismatchException at…
Dan S.
  • 87
  • 3
  • 5
  • 11
1
2
3
17 18