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

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -