Java Error “Exception in thread ”main“ java.util.InputMismatchException” On an Array program

I recently typed out this java program to accept ten areas and their pin-codes and then search to find a particular area and print out it’s pin-code. Here’s the code from the program :

import java.util.Scanner;
public class Sal {

    public static void main (String args []){ 
        Scanner s=new Scanner(;
        System.out.println("Enter 10 areas and their pincodes");
        String area[]=new String [10];
        int pincode[]=new int [10];
        String search;
        int chk=0;
        int p=0;

        for (int i=0;i<=9;i++){

        System.out.println("Enter Search"); 

        for (int j=0;j<=9;j++){

            System.out.println("Search Found "+"Pincode : "+pincode[p] );
        } else {
            System.out.println("Search not Found");

And after entering two areas I get this ERROR:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Sal.main(

Can someone please tell me what I’m doing wrong! :/ Any help is appreciated.

6 Answers

First of all, remember to indent your code for readability.

Concept 1.

for (int i=0;i<=9;i++){

area[i];// Use this for String Input


s.nextLine();//Use this for going to next line of input


Concept 2.


// compare Strings using compareTo method (which returns 0 if equal

Rest of your code and concepts are correct 🙂

From the docs for Scanner#nextInt():

InputMismatchException – if the next token does not match the Integer regular expression, or is out of range

So, it sounds like your Scanner is trying to read in an int but getting something that it can’t turn into an int (either what it read is not a number or the number is too large).

You call the relevant function here:

for (int i=0;i<=9;i++){
    pincode[i]=s.nextInt(); // <-- the culprit

My guess is that at some point, your call to .nextLine() swallows up an entire line, and the next line starts with an “area”. I can’t do more without knowing how you expect the input to be formatted.

From InputMismatchException’s JavaDoc:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

It seems that you entered a string whereas nextInt() expects an integer.

I’m assuming the error happens on the line pincode[i]=s.nextInt(); (which is line 14). The reason this happens is because the input (from cannot be parsed as an int. Are you sure you’re entering correct values?

The input can not be parsed as an integer. Maybe you have a comma at the end of line.



is bad practice to check string equality. use search.equals(area[j]) with null-check.

I’m new to Programming. However, I faced the similar issue to over come while creating customers for bank (its just a practice problem). To overcome the problem I have created separate scanners for each input type & Closed all the scanners at the end of program. It worked.

Leave a Reply

Your email address will not be published. Required fields are marked *