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
votes
1 answer

catch block is running multiple times for InputMismatchException

So I was using this code in my program and whenever I give input consisting of multiple words, the compiler executes the catch block that many times. I've also tried it with different methods & till now all efforts went to vain. Method 1: Scanner…
JDK
  • 125
  • 2
  • 14
-1
votes
1 answer

java.util.InputMismatchException When loading from file

Hi so I am trying to use this code to read from a text file, the text file is supposed to define a pitch by checking if the string of the next line starts with either stop, play, or duration and then pass it to synth so it can play. Does anyone have…
-1
votes
1 answer

How to keep catching an exception until it's correct java

I'm writing a program that uses the Scanner class and want to use a try-catch block to catch InputMismatchExceptions. This is what I have written: public class StudentInfo{ public static void main(String[] args){ Scanner scnr = new…
-1
votes
1 answer

How to use an InputMismatchException object

I have an array of a specified sized created and must ask the user to enter an index and print that indexed value. However if they enter something that's not an int I need to use an InputMismatchException to check whether they've entered 'q' or 'Q'…
-1
votes
1 answer

How to resolve InputMismatchException?

package exercises; import java.util.*; public class Try_and_catch { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x=1; do { System.out.println("Enter first…
-1
votes
3 answers

Catch isn't catching InputMismatchException?

So, I've searched around google and stackoverflow for a bit, but I can't seem to find an answer to this issue. I've got 2 methods. The first method, getAge is meant to just get an integer as input from the user. It's then meant to pass that input…
Will Troll
  • 23
  • 1
  • 1
  • 3
-1
votes
2 answers

How do I make sure that a string value is not passed into an int variable?

I am still very new to programming. I've only taken three programming classes, one in C++ and two in Java. I decided I wanted to build my own version of Battleship. I've more or less got the entire project finished. My issue has been providing…
Paweth
  • 3
  • 2
-2
votes
2 answers

Why try/catch InputMisMatchException doesnt work even if I import java.util.InputMisMatchException?

I am trying to write simple calculator and implement some exceptions. I want to catch exception InputMisMatchException if client will try to input letter instead number. I have already import java.util.Input... but this still doesnt work and it end…
Matirix
  • 1
  • 2
-2
votes
2 answers

Day 8: Dictionaries and Maps JAVA

I'm trying to solve this problem https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem?h_r=next-challenge&h_v=zen using linked list and ind it's successfully solved in eclipse with all the outputs correct like hackerrank ouputs but…
A.Usama
  • 157
  • 2
  • 8
-2
votes
2 answers

Try Catch InputMismatchException not actually catching my Error

I have copied the exact same code from another program. Whenever inputting deliberate incorrect results the InputMismatchException still occurs and the program crashes. import java.util.*; public class Runner{ public static void main (String…
user9312408
-2
votes
1 answer

How to solve InputMismatchException with Integer and char?

I got this lines : while(flag==0){ try{ code=input.nextInt(); } catch(java.util.InputMismatchException z) { System.out.print("\nDigito(s) Invalidos!\n"); flag=1; } finally { …
Escaga Seen
  • 131
  • 11
-2
votes
4 answers

Java Try and Catch inputMismatchException Error

I have the following code //Ask for weight & pass user input into the object System.out.printf("Enter weight: "); //Check make sure input is a double weight = input.nextDouble(); weight = checkDouble(weight); …
Kyl
  • 45
  • 2
  • 10
-2
votes
3 answers

Try Catch Block Not Catching String Input (InputMismatchException)

I am needing to stop the user from entering a string value. Here is what I've tried so far. import java.util.InputMismatchException; import java.util.Scanner; import java.util.Random; public class guessinggame { public static void main…
sawreals
  • 338
  • 4
  • 15
-2
votes
2 answers

Exception in thread "main" java.util.InputMismatchException

I`m MAC user and I have been struggling with this error. I search this error, now I just can't find solutions. Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at…
백지희
  • 1
  • 1
  • 1
  • 1
-3
votes
3 answers

Exception in thread "main" java.util.InputMismatchException when calculating Salary in Java

I'm trying to create a program where the user can calculate their daily salary based on when they arrived and left work. I have however received the same error message for the past hour, any help would be greatly appreciated. Exception in thread…
Spyro
  • 11
  • 5
1 2 3
17
18