Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

You must login to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Passionable Logo Passionable Logo
Sign InSign Up

Passionable

Passionable Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions
Home/ Questions/Q 7041
In Process
Alek Richter
  • 0
Alek RichterEnlightened
Asked: December 23, 20212021-12-23T11:13:05+00:00 2021-12-23T11:13:05+00:00

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

  • 0

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.

  • 1 1 Answer
  • 6 Views
  • 0 Followers
  • 0
    • Report
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Alek Richter Enlightened
    2021-12-23T11:13:43+00:00Added an answer on December 23, 2021 at 11:13 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 4k
  • Best Answers 0
  • Users 3
  • Popular
  • Answers
  • Alek Richter

    Truth value of a Series is ambiguous. Use a.empty, a.bool(), ...

    • 2 Answers
  • Alek Richter

    What is a NullPointerException, and how do I fix it?

    • 2 Answers
  • Alek Richter

    Make MS Word document look like it has been typeset ...

    • 2 Answers
  • Alek Richter
    Alek Richter added an answer Pandas DataFrame columns are Pandas Series when you pull them… January 13, 2022 at 2:21 pm
  • Alek Richter
    Alek Richter added an answer The handshake failure could have occurred due to various reasons:… January 13, 2022 at 2:19 pm
  • Alek Richter
    Alek Richter added an answer Mac OS X doesn't have apt-get. There is a package… January 13, 2022 at 2:18 pm

Top Members

Alek Richter

Alek Richter

  • 4k Questions
  • 1k Points
Enlightened
coinbaseprosupportnumber[Xsdwerfgtyujhnbgfderewqasdxz]

coinbaseprosupportnumber[Xsdwerfgtyujhnbgfderewqasdxz]

  • 2 Questions
  • 2 Points

Trending Tags

questin question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions

© 2021 Passionable. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.