I wrote a Java program to accept ten areas and their pin codes, then search for a specific area and display its pin code. However, after entering two areas, I get this error:
Exception in thread “main” java.util.InputMismatchException
Here’s my code:
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");
}
}
}
The error occurs at pincode[i] = s.nextInt();
. What is causing this java.util.InputMismatchException, and how can I fix it?
Oh, I’ve run into this issue before! The error is happening because nextInt()
doesn’t consume the newline character after reading the integer. So, when nextLine()
is called next time, it ends up reading the leftover newline instead of the expected area name.
The java.util.InputMismatchException is essentially a mismatch between what you’re trying to input and what the program expects. To fix this, after calling nextInt()
, you need to consume that leftover newline. Here’s a quick fix:
for (int i = 0; i <= 9; i++) {
area[i] = s.nextLine();
pincode[i] = s.nextInt();
s.nextLine(); // Consume the leftover newline
}
Pros: This fix is super simple and requires minimal changes to your code.
Cons: It doesn’t deal with cases where the input might not be an integer, which could still cause issues later on.
That’s a great fix! But I’d like to add a bit more robustness to the input handling. If there’s a chance that the user could input something other than a number for the pincode, it could lead to the java.util.InputMismatchException again. To prevent this, you can catch the exception and prompt the user to re-enter a valid pincode.
Here’s how you can do that:
for (int i = 0; i < 10; i++) {
System.out.print("Enter area: ");
area[i] = s.nextLine();
while (true) {
System.out.print("Enter pincode: ");
try {
pincode[i] = Integer.parseInt(s.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Invalid pincode! Please enter a valid number.");
}
}
}
Pros: This will prevent the program from crashing due to invalid input and ensures smoother interaction with the user.
Cons: It adds a bit more logic, but it’s well worth it for the user experience!
You’ve got the input issue under control now, but I noticed another little bug in your code that could cause trouble later: the way you’re comparing strings.
In Java, ==
checks for reference equality, not content equality. Since area[j]
is a String
object, using ==
might not give the expected result. To compare string values, use the .equals()
method instead.
Here’s the corrected part of your search logic:
if (search.equals(area[j])) // ✅ This correctly compares string values
Pros: This ensures that the program correctly checks if the search string matches any area.
Cons: It’s a small detail, but you just need to remember to use .equals()
when comparing strings.