java - Scanner shows exception InputMisMatchException -
static long arraymaxmin[]; public static void main(string[] args) { try{ scanner in = new scanner(system.in); long[] arr = new long[5]; for(int arr_i=0; arr_i < 5; arr_i++){ arr[arr_i] =long.valueof(math.abs(in.nextlong())); } arraymaxmin = calculate(arr); }catch(inputmismatchexception e) { e.getmessage(); } try{ long[] array = findmaxmin(arraymaxmin); for(int ij=0;ij<array.length;ij++){ system.out.print(array[ij]+" "); } }catch(nullpointerexception e){ e.getmessage(); } } public static long[] calculate(long[] arraymaxmin ){ int max=0,min=0; long[] outputarray={0,0,0,0,0}; for(int i=0;i<arraymaxmin.length;i++){ for(int j=0;j<arraymaxmin.length;j++){ if(i==j){ //do nothing }else { outputarray[i] = outputarray[i]+arraymaxmin[j]; } } } return outputarray; } public static long[] findmaxmin(long[] arr){ long max=0,min=0; try{ for(int i=0;i<arr.length;i++){ if(max<arr[i]){ max= arr[i]; }else{ min= arr[i]; } } long output[]={min,max}; return output; }catch(nullpointerexception e){ e.getmessage(); return null; } }
well trying hackerrank question here, , don't possible reason getting testcases failing, in min-max problem.
i don't it, lacking here or case lacking in ,please me ,well presume lacking if input such of 7777777777777777777777 fails, can problem goes like
given 5 positive integers, find minimum , maximum values can calculated summing 4 of 5 integers. print respective minimum , maximum values single line of 2 space-separated long integers.
input format
a single line of 5 space-separated integers.
constraints
each integer in inclusive range . {1,10^9}
print 2 space-separated long integers denoting respective minimum , maximum values can calculated summing 4 of 5 integers. (the output can greater 32 bit integer.)
as can see, minimal sum , maximal sum . thus, print these minimal , maximal sums 2 space-separated integers on new line.
hints: beware of integer overflow! use 64-bit integer.
the fisrt thing wrong here fact swallowing exeptions...
here:
catch(inputmismatchexception e) { e.getmessage(); }
should
catch (inputmismatchexception e) { e.printstacktrace(); }
then able see why app crashing:
java.util.inputmismatchexception: input string: "7777777777777777777777"
this input: 7777777777777777777777 not valid long...
that why got professor hint: use 64-bit integer. 7777777777777777777777 bigger that... invalid input making scanner crash trying read long..
Comments
Post a Comment