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

InputMismatchException occurred when inputing String and Double values

Here is my problem I've been struggling with: I want the input to be in this form: 1 So I wrote the code like this. Unfortunately, it either gives me an InputMismatchException at line 6 or cannot break at "END_PRICES". Scanner input =new…
Tina
  • 13
  • 3
0
votes
2 answers

Is there a way to only get the value from an input method to another method without repeating the whole process again in java?

I really want to know if there is a way to only get the value from the method to an another method. Because in the main method the first method is already call out while the the second needed the value of the input from the first method. //Main…
0
votes
1 answer

How to Fix java.util.InputMismatchException if the data types seem to match the scanner input?

for my code I'm writing to take some input from scanner (an int first, then a string, and finally a double), I can only get the code to compile when I take out the lines for int and the double. Otherwise I get this error: and even when I take out…
JG98
  • 43
  • 5
0
votes
1 answer

How to avoid InputMismatchException in java while taking input in int,char and int

I am new to Java and I want to solve a simple problem in java. In input I need to take an integer a and then a character c and then another integer b And print the output if the character is '+' then print a+b And so on like this. The input looks…
CSE
  • 73
  • 6
0
votes
1 answer

InputMismatchException in a scanner to get data from txt file

I'm trying to read the highscores of a game from a txt file and put them into an array so that another section of the program can detect a high score. Earlier I had everything declared as Int and it worked fine but I wanted more precision so I…
0
votes
1 answer

Java - Input Mismatch Exception?

I'm still learning java so excuse my lack of skill here, any help is appreciated! I've been working on this java I/O project for school all day at this point and now I'm getting this input mismatch error on line 73 qtr = inputFile.nextInt();. The…
0
votes
1 answer

input mismatch exception while using space in between words

I'm working on a project and I already finished it, I have really simple problem which makes me really confused. I'm trying to ask a user to enter a number from a menu and depending on that different things happen, but I get input mismatch exception…
0
votes
1 answer

How to overcome Exception in thread "main" java.util.InputMismatchException

Program: import java.util.Scanner; public class JavaExercise1 { static int sum, num1, num2; static Scanner scan = new Scanner(System.in); public static void add(){ System.out.print("Enter first number to be added: "); …
0
votes
4 answers

How to prevent wrong user input on a variable?

Im currently trying to build a code that prevents wrong user input (storing a char/string to a integer, e.g.) but Im not being able to make it to work... public static void main(String[] args) { int num=0, option=0; boolean keep=true; …
Agustin
  • 3
  • 5
0
votes
1 answer

How am I catching an InputMismatchException here?

I'm trying to catch InputMismatch exceptions and display a message that tells me where the exception occurred and what type of input is required. I need this to loop until the correct input is entered. I'm doing this by incrementing "statusCode" and…
0
votes
1 answer

Need to create an input mismatch exception

I want to put a try/catch around the input=scanner.nextInt to throw an InputMismatchException if a letter is entered, I keep getting an infinite loop. This is the code with the infinite loop. What would be best to fix this? public void…
0
votes
1 answer

Do I have to change my code to use a scanner in order for inputmismatch exception to work?

My InputMismatchException isn't working, as when I put in something like a double into the input, instead of the exception output message, it gives me: Exception in thread "main" java.lang.NumberFormatException: For input string: "1.2" at…
gino
  • 41
  • 5
0
votes
1 answer

InputMismatchException need more inputs?

I am receiving a InputMismatchException java ShoppingTime Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at…
Willy
  • 57
  • 7
0
votes
2 answers

Stop infinite Loop when handling invalid user input - Java

I'm fairly new to programming and I'm having trouble with my program continuously looping when I try to handle a InputMisMatch exception. I have a menu which takes in user input and then uses a switch statement to deal with the input. I am trying to…
0
votes
2 answers

I keep getting an InputMismatchException even though all types are correct , what should ido?

import java.util.Scanner; public class Messdaten { public static double temperaturInDurchschnitt(Scanner sc){ int year= 0, month= 0 , day= 0; String discription = ""; double value= 0.0; double warmest= -273.15; …