java - UVA Online Judge is returning runtime error that I can't find -
i've been attempting problem 100 on uva:
however, keeps telling me code has runtime errors can't reproduce on end. works fine on console. i've tried solutions other people have, it's still not working. got rid of public methods specified.
what other possible reasons there getting issue?
here code:
import java.util.*; class main { public static void main(string args[]){ int =0; int j=0; scanner input = new scanner(system.in); = input.nextint(); j = input.nextint(); while((i!=0)&&(j!=0)) { int tempmax = 0; for(int k =i; k <= j; k++){ if (tempmax<algstep(1,k)) tempmax=algstep(1,k); } system.out.println(i + " " + j + " " + tempmax); = input.nextint(); j = input.nextint(); } } static boolean iseven(int n){ if(n%2==0) return true; else return false; } static boolean isone(int n){ if(n==1) return true; else return false; } static int algstep(int count, int n){ int newcount; int m; if(isone(n)) return count; if(iseven(n)){ m = n/2; newcount = count+1; } else{ m = (3*n)+1; newcount = count+1; } return algstep(newcount, m); } }
ok, did research you.
one finding: uva online judge bad in helping learn programming language. tells there runtime error, cannot see happened exactly.
to analyze problem, must change program setup little.
the main program reads data standard input stream (aka console). assume did not input data in proper format. mean, in same format uva does. having exact result, should put input data file. create text file content specification page (the 4 lines 2 integers each).
change line
scanner input = new scanner(system.in); to
scanner input = new scanner(new fileinputstream(args[0])); then run program absolute path input file first program argument. example "java main input.txt".
you following output
1 10 20 100 200 125 201 210 89 900 1000 174 exception in thread "main" java.util.nosuchelementexception @ java.util.scanner.throwfor(scanner.java:907) @ java.util.scanner.next(scanner.java:1530) @ java.util.scanner.nextint(scanner.java:2160) @ java.util.scanner.nextint(scanner.java:2119) @ main.main(main.java:29) the problem in line
i = input.nextint(); which inside loop. here reading next integer, regardless whether input file has 1 or not. see anser of tony_craft. have take care of that.
Comments
Post a Comment