Java: Incompatible types: ArrayList<Integer> cannot be converted to int -
i'm getting error states arraylist cannot converted int on return arr
i'm writing program in have read ints file , calculate sum. i'm handling exceptions, if exception thrown, function should return 0.
here code:
public static int intfilesum(string filename) { arraylist<integer> arr=new arraylist<integer>(); inputstream fis=new fileinputstream(filename); scanner s=new scanner(fis); try { while (s.hasnextint()) { arr.add(s.nextint()); } } catch (filenotfoundexception e) { return 0; } catch (ioexception f) { return 0; } { fis.close(); s.close(); return arr; } }
here unit test should pass once file read:
public void testsumintshappypath() throws exception { int sum=assignment11.intfilesum("inputs/ints1.txt"); assert.assertequals(10, sum); sum=assignment11.intfilesum("inputs/ints2.txt"); assert.assertequals(56, sum); }
any appreciated.
thanks!
why need arraylist?
you want sum numbers, that.
public static int intfilesum(string filename) { int sum = 0; inputstream fis=new fileinputstream(filename); scanner s=new scanner(fis); try { while (s.hasnextint()) { sum += s.nextint();
and then, return sum
Comments
Post a Comment