java - Code runs fine in Eclipse but throws NumberFormatException in CodeChef -
the code working fine on eclipse ide on codechef, compiler showing error:
exception in thread "main" java.lang.numberformatexception: null @ java.lang.integer.parseint(integer.java:542) @ java.lang.integer.parseint(integer.java:615) @ codechef.main(main.java:19)
here's code:
import java.util.*; import java.lang.*; import java.io.*; class codechef { public static void main(string[] args) throws ioexception { int x = 0, j = 0; string s; int counta = 0, countb = 0; int countf[] = new int[5]; bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); int = integer.parseint(br.readline()); while (j < i) { s = br.readline(); (int k = 0; k < s.length(); k++) { if (s.charat(k) == 'a') { counta++; } else { countb++; } } if (counta < countb) { countf[j] = counta; } else { countf[j] = countb; } j++; counta = countb = 0; } (int g = 0; g < i; g++) { system.out.println(countf[g]); } } }
i tried using scanner class in showing nosuchelementexception
.
your problem here:
int = integer.parseint(br.readline());
you trying parse integer input not validating if input is can parsed integer
you need validate input , go forward when input correct is:
example
int = -1; while (i == -1) { try { = integer.parseint(br.readline()); } catch (numberformatexception e) { system.out.println("wrong input"); } }
Comments
Post a Comment