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
0 answers

Why does my scanner throw an InputMismatchException in this case?

I'm quite new in programming and for an assignment I need to scan in Coordinates. The input is given as follows: 5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4 And I am currently trying to get my scanner working. I've used two while loops…
0
votes
1 answer

InputMismatch exception reading boolean values

I have a file with some values in it: 11 8 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 1 0 0 0 I need to read those values…
Tony
  • 29
  • 8
0
votes
1 answer

Java scanner delimiter error csv java end of line

I'm receiving an InputMismatchException while scanning in the last int of a csv. public class TradeSim { public void readFile(String fileName) { try { // file name and absolute path File file = new…
andrewkeithly
  • 133
  • 11
0
votes
2 answers

Scanner throws InputMismatchException with null

import java.io.*; import java.util.*; public class Main{ public static void main(String [] args) throws InputMismatchException{ double width; int period; double Ppp; Scanner in0 = new Scanner(System.in); Scanner in1 = new…
psofoklis11
  • 37
  • 1
  • 6
0
votes
2 answers

When Scanner.nextLong() throws InputMismatchException from try block then how does the Scanner.next() get the same value in catch block?

when i run the following code in java: import java.util.*; class Solution{ public static void main(String []argh) { Scanner sc = new Scanner(System.in); try{ long x=sc.nextLong(); …
cnova
  • 725
  • 2
  • 9
  • 22
0
votes
2 answers

InputMismatchException: String not recognized

I wrote the piece of Java code below. When running it and typing in any value (either one defined, e.g. latte, or any other, e.g. az integer), I get an InputMismatchException. As far as I could find answers, this exception means that the input type…
Gabor
  • 43
  • 1
  • 1
  • 5
0
votes
2 answers

Cannot Figure out how to catch InputMismatchException

So here is my current code for catching an InputMismatchException error int weapon = 0 boolean selection = true; while(selection) { try { System.out.println("Pick number 1, 2, or 3."); weapon = scan.nextInt(); selection…
Syrobot
  • 3
  • 1
0
votes
1 answer

InputMismatchError when scanning from file

I'm trying to read data in from a .txt file into my program and it's throwing InputMismatchErrors despite it looking like it shouldn't. The error **********HELLO*********** Which animal are you looking to check into the kennel?: Dog Cat cat //typed…
James Gould
  • 4,492
  • 2
  • 27
  • 50
0
votes
0 answers

InputMismatchError after implementing boolean

I've implemented a boolean to a kennel system I'm developing in Java and I'm getting an InputMismatchError when loading the data from the file. I've read through a few times and tried to work it out but the solutions I'm trying aren't working. So…
James Gould
  • 4,492
  • 2
  • 27
  • 50
0
votes
2 answers

InputMismatchException when entering integers multiple times

I have seen other similar questions and didn't find any solution to my problem. Just trying to scan 2 numbers and add them together: Scanner input = new Scanner(System.in); int number1; int number2; int sum; System.out.print("First: "); number1 =…
0
votes
1 answer

InputMismatchException when scanning file of integers

When I run this code, I get an InputMismatchException (see comment). Why does this happen? The file I want to read contains a list of integers separated by spaces. Below is just the main method. public static void main (String[] args) throws…
0
votes
0 answers

Ways of Avoiding InputMismatchException & NoSuchElementException?

Apart from regular expressions, parsing Strings as ints and try/catch blocks are there any other ways of handling InputMismatchException & NoSuchElementException with a Scanner? import java.util.Scanner; public class TestingScanner { public…
0
votes
2 answers

Scanner.next... throws java.util.InputMismatchException for Float but not for Int

Why does Java throw an error when using Scanner.nextFloat() but not Scanner.nextInt() ? package myshit; import java.lang.Math; import java.util.Scanner; public class speed2 { public static Scanner keyboard = new Scanner(System.in); public…
0
votes
2 answers

InputMismatchException...even with try/catch

So I'm writing code here just for fun but I've come up with an error that I just can't seem to fix. This block of code is supposed to take in an int... at first i had the hasNextInt() in the while loop alone to try and ensure i'm getting the correct…
0
votes
1 answer

How to display an error message if user input is not in the correct form?

So far in my code I prompt the user to enter a positive integer representing the number of people they are inviting to an event. I already have an if statement to return an error message if the user input is a negative value. But how do I return an…
Clarisa
  • 125
  • 2
  • 12