Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

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.(5)

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.

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

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error: exception in thread "main" java.util.inputmismatchexception - Quick tips
Next
Answered
Inès Renault
  • 23
Inès Renault
Asked: May 11, 20222022-05-11T18:37:13+00:00 2022-05-11T18:37:13+00:00In: Programs

Error: exception in thread “main” java.util.inputmismatchexception – Quick tips

  • 23

. Advertisement .

..3..

. Advertisement .

..4..

This is the program I run:

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");
  }
  }
 }

After I run, it returns an 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)

Does anyone have any suggestions for my problem: exception in thread “main” java.util.inputmismatchexception in the programs – How to correct it?

exception in thread "main"
  • 2 2 Answers
  • 90 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-05-28T15:52:40+00:00Added an answer on May 28, 2022 at 3:52 pm

    The cause: The input does not match the Integer regular expression, or is out of range. I guess that at some point, your call to .nextLine() swallows up an entire line, and the next line starts with an “area”.

    Solution: 

    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
    
    }

    Compare Strings with the compareTo method (which returns zero if they are equal)

    if(search.compareTo(area[j])==0){ 
    • 21
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Léna Mallet
    2022-05-25T19:25:23+00:00Added an answer on May 25, 2022 at 7:25 pm

    The Scanner#nextInt() docs:

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

    It sounds like your Scanner has tried to read in a int, but it is getting something it cannot turn into a int (either the number it read isn’t readable or it is too large).

    This is the function you call:

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

    I suspect that .nextLine()‘s call takes up a whole line. The next line begins with an “area”. Without knowing what format you want the input to look like, I can’t do much more.

    • 15
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. 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.