Sum file txt using scanner java -



Sum file txt using scanner java -

i have info in file file.txt :1,2,3 , want sum of info text. how should proceed?

int a[] = new int [5]; int h=0; string filename = "c:/users/asus/desktop/numbers.txt"; file textfile = new file (filename); scanner in = new scanner (textfile); while (in.hasnextline()) { string line = in.nextline(); system.out.println(line);`enter code here` } (int = 0; i<a.length; i++) { h +=a[i]; } in.close(); system.out.print("sum : " + h); }

thank you!

use nextint() method each integer in iteration. (i considering have integers in file)

initialize sum variable 0 , produce sum @ each iteration.

example

int sum = 0; while(in.hasnextint()){ sum +=in.nextint(); }

there might me minor errors idea. when loop stops, have sum in sum variable

java

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -