Error Exception In Thread Main Java.util.inputmismatchexception
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Exception in thread main java.util.InputMismatchException error up vote 0 down vote favorite I have a question on what's going on, whenever I try to compile it it keeps giving me an error like this: 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 Person.main(Person.java:38) All I want is for the user to be able to input their age and name and have it stored in the "age" and "name" variables, then have it print it out in the bottom string. And if someone would like to help me clean up my code as well, it wouldn't hurt.. import java.util.*; import java.io.*; import java.util.Scanner; public class Person { public static void main(String[]args) { int age; int name; Scanner scan = new Scanner(System.in); System.out.println("Enter in your age."); age = scan.nextInt(); if (age < 18) { System.out.println("So you're a kid, huh? That's fine."); } else if (age >= 18) { System.out.println("Ah, and adult! Good."); } @SuppressWarnings("resource") Scanner in = new Scanner(System.in); System.out.println("Enter in your name"); name = in.nextInt(); System.out.println("So you're " + age + " years old and your name is " + name); } } java util share|improve this question asked Mar 6 '13 at 14:13 Xiam 1303512 add a comment| 3 Answers 3 active oldest votes up vote 1 down vote accepted Issues int name; //Name should be of type String ... System.out.println("Enter in your name"); name = in.nextInt(); //It doesn't handle the string since your using `nextInt` Solution String name; ... System.out.println("Enter in your name"); name = in.nextLine(); share|improve this answer edited Mar 6 '13 at 14:20 answered Mar 6 '13 at 14:15 Kevin Bowersox 63.1k786114 1 Keep in mind that the OP defines name as an int. name should be defined as a string. –ᴍᴀsᴛᴇʀᴍɪɴᴅ
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just http://stackoverflow.com/questions/15249578/exception-in-thread-main-java-util-inputmismatchexception-error like you, helping each other. Join them; it only takes a minute: Sign up Java Error “Exception in thread ”main“ java.util.InputMismatchException” On an Array program up vote 2 down vote favorite 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 http://stackoverflow.com/questions/18407860/java-error-exception-in-thread-main-java-util-inputmismatchexception-on-an-a 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. java arrays share|improve this question edited Aug 23 '13 at 16:48 redFIVE 1,1352919 asked Aug 23 '13 at 16:41 user2711784 28246 add a comment| 5 Answers 5 active oldest votes up vote 3 down vote accepted First of all, remember to indent your code for readability. C
This Site Careers Other all forums Forum: Beginning Java Exception in https://coderanch.com/t/516226/java/java/Exception-thread-main-java-util thread "main" java.util.InputMismatchException john bullwinkle Greenhorn Posts: 4 posted 5 years ago i'm unsure of why i'm getting this error. https://www.daniweb.com/programming/software-development/threads/359055/scanner-and-nextint-problem from the text file i'm readin in, the places all match up, so i'm not sure what's throwing it off. here error exception is my code Scanner src = new Scanner(fin); src.useDelimiter(","); int a = 0; int x = 0; while(src.hasNext()){ monthlyOrDaily[a] = src.nextInt(); System.out.println("monthlyOrDaily = " + x); formDisplayName[a] = src.next(); System.out.println("formDisplayName =" + formDisplayName[a]); formFileName[a] = src.next(); System.out.println("formFileName = " + formFileName[a]); formSaveOptions[a] error exception in = src.next(); System.out.println("formSaveOptions = " + formSaveOptions[a]); endFileName[a] = src.next(); System.out.println("endFileName = " + endFileName[a]); System.out.println("a = " + a); a++; } here is the text file i'm reading 1,LP Carbon Monoxide Call Form,LPCarbonMonoxideCallForm_distributed,Salesman Number-Tank Description-Other Item,CrbnMonCall, 1,Driver Vehicle Inspection Report,DriverVehicleInspectionReport_distributed,Salesman Number-Tank Description-Other Item,DrvrVehInspct, 1,LP Driver Sales Hours Record,LPDriverSalesHoursRecord_distributed,Salesman Number-Tank Description-Other Item,DrvSlsHrs, 1,LP Equipment Calibration Form,LPEquipmentCalibrationForm_distributed,Salesman Number-Tank Description-Other Item,LPEqpmntCalbrt, 1,LP Equipment Lease,LPEquipmentLease_distributed,Salesman Number-Tank Description-Other,ItemLPEqpLse, 1,LP Montly Sales Checklist For Service Techs,LPMonthlySalesChecklistForServiceTechs_distributed,Salesman Number-Tank Description-Other Item,MthlSlsChkLstTch, 1,LP Out of Gas Form,LPOutOfGasForm_distributed,Salesman Number-Tank Description-Other Item,OutGas, 1,LP Tank Cart Inspections,LPTankCartInspections_distributed,Salesman Number-Tank Description-Other Item,TnkCrtInsp here is the error log: monthlyOrDaily = 1 formDisplayName =LP Carbon Monoxide Call Form formFileName = LPCarbonMonoxideCallForm_distributed formSaveOptions = Salesman Number-Tank Description-Other Item endFileName = CrbnMonCall a = 0 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Sc
storer, I use Scanner, and its .nextInt(); to ask the user for the employee's contact number: Scanner Scan = new Scanner(System.in); out.print("\nEnter Employee Contact Number:"); newEmployee.contactNumber = Scan.nextInt(); //(I have a class Employee) I get the error : Enter Employee Contact Number:456645 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 employeestore.EmployeeStorer.addEmployee(EmployeeStorer.java:55) at employeestore.EmployeeStorer.main(EmployeeStorer.java:25) I have done about half an hour of google-ing and I am currently *extremely* annoyed. Any help would be helpful. Thanks. . Dannyo329 10 79 posts since Apr 2008 Community Member 4Contributors 6Replies 8Views 5 YearsDiscussion Span 5 Years Ago Last Post by Dannyo329 0 JamesCherrill 2,728 5 Years Ago After a InputMismatchException the Scanner still contains the offending text, so you can catch the exception, then in the catch block use Scan.next() to see exactly what it was that it objected to. 0 Discussion Starter Dannyo329 10 5 Years Ago I don't know how to use try and catch yet. Can you post that piece of code? 0 javaAddict 900 5 Years Ago Did you enter an integer number? Usually when I got that exception with scanner it was because of that. Because I see this at what you posted:Enter Employee Contact Number:456645 Are you sure that is where you get the error. The exception tells you where you got that error: Enter Employee Contact Number:456645 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) [B] at employeestore.EmployeeStorer.addEmployee(EmployeeStorer.java:55)[/B] at employeestore.EmployeeStorer.main(EmployeeStorer.java:25) at employeestore.EmployeeStorer.addEmployee(EmployeeStorer.java:55) Can you post some more code before and after the line: 25. Also indicate in your code that line. 0 cretaros 5 Years Ago try this syntax: try { .....put ur code here.. } c