Jump to content

Java InputMismatchException

prolemur

In my program when the user doesn't enter an integer, the program hits and error. How can I make sure the user inputs an integer without it crashing?

import java.util.Scanner;public class main {	public static void main(String[] args) {				Scanner in = new Scanner(System.in);				System.out.print("Enter the length: ");		int length = in.nextInt();	}}
Link to comment
Share on other sites

Link to post
Share on other sites

You're trying to convert their input to an integer without checking it's actually an integer.

 

A quick google gives this stackoverflow answer which should help http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java

 

What you want to do after that is while(input is not an integer) { prompt user to input an integer } so you ask them to input an integer until they input an integer.

Link to comment
Share on other sites

Link to post
Share on other sites

In my program when the user doesn't enter an integer, the program hits and error. How can I make sure the user inputs an integer without it crashing?

 

import java.util.Scanner;public class main {	public static void main(String[] args) {				Scanner in = new Scanner(System.in);				System.out.print("Enter the length: ");		int length = in.nextInt();	}}

 

Changing the code to something like this, will alleviate the problem:

 

import java.util.Scanner;public class main {    public static void main(String[] args) {                Scanner in = new Scanner(System.in);                int length;        boolean finished = false;        while (!finished) {            try {                System.out.print("Enter the length: ");                length = in.nextInt();                finished = true;            } catch (InputMismatchException ime) {                System.out.println("Couldn't read your input. Try again?");            }        }    }}

In the event of an exception in in.nextInt(), the flow will travel to the catch block and not set finished equal to true and will result in the loop beginning again until good input is sent in. However, this may still not give you what you want.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Changing the code to something like this, will alleviate the problem:

 

import java.util.Scanner;public class main {    public static void main(String[] args) {                Scanner in = new Scanner(System.in);                int length;        boolean finished = false;        while (!finished) {            try {                System.out.print("Enter the length: ");                length = in.nextInt();                finished = true;            } catch (InputMismatchException ime) {                System.out.println("Couldn't read your input. Try again?");            }        }    }}

In the event of an exception in in.nextInt(), the flow will travel to the catch block and not set finished equal to true and will result in the loop beginning again until good input is sent in. However, this may still not give you what you want.

This works, but the infinite loop make it run non-stop.

Link to comment
Share on other sites

Link to post
Share on other sites

This works, but the infinite loop make it run non-stop.

 

you could just use hasNextInt() method of Scanner

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

This works, but the infinite loop make it run non-stop.

 

Right, because it isn't doing quite what you want it to. I'll show you another example that does do what you want it to.

 

You need to parse the integer within the scanned input using the Integer wrapper class, like so:

import java.util.Scanner;public class main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        String strLength;        int length;        boolean finished = false;        while (!finished) {            System.out.print("Enter the length: ");            strLength = in.nextLine(); // or in.next() if each integer isn't on its own line            try {                length = Integer.parseInt(strLength);                finished = true;            } catch (NumberFormatException nfe) {                System.out.println("Input wasn't an integer! Try again?");            }        }    }}

As you can see, this method removes the need to catch the InputMismatchException but it adds a NumberFormatException from the Integer.parseInt() method. This should work exactly as you want it to.

Link to comment
Share on other sites

Link to post
Share on other sites

you could just use hasNextInt() method of Scanner

How do I do that, this returns a boolean value.

Link to comment
Share on other sites

Link to post
Share on other sites

How do I do that, this returns a boolean value.

 

You would need a conditional statement using hasNextInt(), if it does have a next int, you can then assign it, but you likely won't because of the way input works in Java. Try my code snippet and tell me if it works.

Link to comment
Share on other sites

Link to post
Share on other sites

You would need a conditional statement using hasNextInt(), if it does have a next int, you can then assign it, but you likely won't because of the way input works in Java. Try my code snippet and tell me if it works.

Yes that works. Is there a quicker way to do this though, seems like a lot of code to check if it's an integer.

Link to comment
Share on other sites

Link to post
Share on other sites

Yes that works. Is there a quicker way to do this though, seems like a lot of code to check if it's an integer.

 

No, there really isn't. At least not one I know of.

Link to comment
Share on other sites

Link to post
Share on other sites

How do I do that, this returns a boolean value.

if(in.hasNextInt()){    length = in.nextInt();} else {    length = -1;}
or

length = in.hastNextInt() ? in.nextInt() : -1;

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×