Java Error "Exception in thread "main" java.util.InputMismatchException" On an Array program -
i typed out java program accept ten areas , pin-codes , search find particular area , print out it's pin-code. here's code 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 , pincodes"); string area[]=new string [10]; int pincode[]=new int [10]; string search; int chk=0; int p=0; (int i=0;i<=9;i++){ area[i]=s.nextline(); pincode[i]=s.nextint(); } system.out.println("enter search"); search=s.nextline(); (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 2 areas error:
exception in thread "main" java.util.inputmismatchexception @ java.util.scanner.throwfor(unknown source) @ java.util.scanner.next(unknown source) @ java.util.scanner.nextint(unknown source) @ java.util.scanner.nextint(unknown source) @ sal.main(sal.java:14)
can please tell me i'm doing wrong! :/ appreciated.
first of all, remember indent code readability.
concept 1.
for (int i=0;i<=9;i++){ area[i]=s.next();// use string input pincode[i]=s.nextint(); s.nextline();//use going next line of input }
concept 2.
if(search.compareto(area[j])==0){
// compare strings using compareto method (which returns 0 if equal
rest of code , concepts correct :)
Comments
Post a Comment