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

How to store values in custom object array using Scanner in Java?

I was given an exam, where I had to create a class and after that, I had to write two functions in which the input parameter is an object Array and took the values from user input. Below is the code that I have written. But, I repetitively got…
0
votes
1 answer

How do I message an end user that they are entering the wrong input and/or format without getting an Exception/Crash?

"Eclipse IDE for Java Developers (includes Incubating components) Version: 2023-03 (4.27.0)" Intro to Programming using Java Section 3.5.3 Sorry for the very long post. This is my first post anywhere about programming. Thanks for any help! I would…
0
votes
1 answer

SQL Error mismatched input 'sql_query' expecting {EOF} when using Create Table in Pyspark

I'm reading sql code from a zip file of text files, saving the query as a variable sql_query, and passing the variable into a spark dataframe like spark.sql('sql_query'). The query text I parse out from the text file functions well using regular sql…
0
votes
0 answers

InputMismatchException when attempting to run program again in do while loop

Having successfully created a policy holder and writing the information to csv I try to run the program again using a do while loop but receive an inputmismatchexception after typing surname. Can someone point me in the right direction or advise…
0
votes
1 answer

Why can't I put a double value for b here?

I tried something like this but public static void main(String[] args){ Scanner Inp = new Scanner(System.in); System.out.println("****Bread Store Inventory****"); System.out.print("Enter the bread amount as the owner of the shop: "); …
0
votes
2 answers

Try/Catch loop issues for paint calculation

The goal is to make code that can handle exceptions and keep going. I made a try/catch for typing string instead of numbers, but when I test it, it prints the catch output but then has these errors : Exception in thread "main"…
0
votes
2 answers

How to take input in java where array length is not defined?

My input is in this format: 1 2 3 4 5 6 Alice The array length is not known. I coded it this way: import java.util.*; public class Main { public static void main(String[] args) { List arr = new ArrayList<>(); Scanner sc…
lsr
  • 264
  • 1
  • 10
0
votes
1 answer

My while loop meant to catch an exception and ask for a new input is repeating infinitly, and I'm not sure why

I'm a very beginner programmer, in my first year of college. Currently, I'm trying to write some Java code to have the user insert a pin number using a scanner, asking for a new input if the pin provided isn't an int. Here's what I've…
0
votes
0 answers

Input Mismatch exception Java

I am getting the input mismatch exception error on my code. I am getting error at the Engine displacement line. Scanner scanner = new Scanner(System.in); Car[] arr; System.out.println("Enter the number of car details to created"); …
0
votes
1 answer

How do I resolve an inputmismatchexception from user whilst using the java try with resources feature

I've found that if I use the try with resources feature java provides which will automatically close of resources, I then can't use a boolean flag (or recursion) to resolve an InputMismatchException within a try catch as the catch block seems to be…
0
votes
2 answers

Difference between two codes and InputMismatchException

I am a newbie with java and the class scanners. I have two Codes and I dont get the point why one of them throws a InputMismatchException. I look forward to the answers. Here both codes: 1st one with the Exception Error: public static void…
0
votes
2 answers

input miss match control in java with try catch

i wrote this code to control input so user cannot enter anything except integers but problem is that: when an Exception occures, the message in Exception block is continousely printed and never ends, what i can do ? public static void main(String[]…
amir
  • 3
  • 2
0
votes
1 answer

The import java.util.InputMismatchException cannot be resolved

I'm unable to catch the InputMismatchException even though I imported java.util.InputMismatchException; At line 2 :: The import java.util.InputMismatchException cannot be resolvedJava(268435846) At line 19 :: InputMismatchException cannot be…
0
votes
2 answers

I am doing my homework and I stuck in one question. I am new to java. can you help me?

The question is: Create a class person with attribute name, age and gender. Create an array of the person to hold 10 person instances. Display the information stored on the person instance in the array. I am not sure what the question says but I…
0
votes
2 answers

InputMismatchException Java first scanner works second does not

This code is working for the first scanner to accept ints but the second scanner throws a mismatch error. I cannot seem to find why it is doing this. Can anyone help me out? I have tried everything and it is not working. I can get the first scanner…