The Huxley: java.util.NoSuchElementException: No line found -
i having problems when submiting answer on https://www.thehuxley.com. when run code on eclipse, goes ok, on huxley, this:
exception in thread "main" java.util.nosuchelementexception: no line found @ java.util.scanner.nextline(scanner.java:1540) @ huxleycode.main(huxleycode.java:12)
and here code itself:
import java.io.*; import java.util.*; public class huxleycode { public static void main(string args[]) { scanner in = new scanner(system.in); int menor = 0, pos = 0, entradas = 0, temp; entradas = in.nextint();// watch out line in = new scanner(system.in); string valores = in.nextline(); entradas = 0; (string val : valores.split(" ")) { temp = integer.valueof(val); if (entradas == 0) { menor = temp; } else if (temp < menor) { menor = temp; pos = entradas; } entradas++; } in.close(); system.out.println("menor valor: " + menor); system.out.println("posicao: " + pos); } }
just complement, in line commented "watch out line", if remove line, scanner ignored nextint() e jumps nextline(), causing error:
exception in thread "main" java.lang.numberformatexception: input string: "" @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65) @ java.lang.integer.parseint(integer.java:592) @ java.lang.integer.valueof(integer.java:766) @ huxleycode.main(huxleycode.java:16)
where mistake, why not work on huxley?
the expected input is:
10 1 2 3 4 -5 6 7 8 9 10
and output:
menor valor: -5 posicao: 4
entradas = in.nextint(); string valores =""; while(in.hasnextline()) valores = in.nextline();
issue nextint()
not set position of scanner beginning of next line, first call return empty string. explained here...
can't use scanner.nextint() , scanner.nextline() together
one thing note works because, reinitialzed scanner forcing start beginning of next line. unfortunately not work huxley send input programmatically @ once , lost losing reference first scanner.
also below should work
entradas = in.nextint(); string valores = in.nextline();//get empty str & set pos of scanner beginning of next line valores = in.nextline();
Comments
Post a Comment