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

64,146

Solution 1

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

Concept 1.

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

area[i]=s.next();// Use this for String Input

pincode[i]=s.nextInt();

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

}

Concept 2.

if(search.compareTo(area[j])==0){ 

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

Rest of your code and concepts are correct :)

Solution 2

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++){
    area[i]=s.nextLine();
    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.

Solution 3

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.

Solution 4

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

btw:

if(search==area[j])

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

Solution 5

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 System.in) cannot be parsed as an int. Are you sure you're entering correct values?

Share:
64,146

Related videos on Youtube

Nikhil Gopal
Author by

Nikhil Gopal

Updated on July 23, 2022

Comments

  • Nikhil Gopal
    Nikhil Gopal almost 2 years

    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.in);
            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++){
                area[i]=s.nextLine();
                pincode[i]=s.nextInt();
            }
    
            System.out.println("Enter Search"); 
            search=s.nextLine();
    
            for (int j=0;j<=9;j++){
                if(search==area[j]){
                    chk=1;
                    j=p;
                    break;
                }
            }
    
            if(chk==1){
                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 java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Sal.main(Sal.java:14)
    

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

  • user1231232141214124
    user1231232141214124 over 10 years
    Its not bad practice, its just plain incorrect in the context he is using it. The double equal operator does NOT check the contents of the strings, it checks if they are the same String objects.
  • Xynariz
    Xynariz over 10 years
    Which, in this particular case, they are guaranteed NOT to be the same String object. The .Equals() method is the ONLY way to compare string equality in Java unless you want to compare memory addresses.

Related