The number of integers to be added is not fixed by the program. The data itself says how many integers are to be added.
Here is the program, except for a blank or two. Think before you click them.
import java.io.*; import java.util.Scanner; class AddUpAll { public static void main ( String[] args ) throws IOException { int value; // the value of the current integer int limit; // the number of integers to add up int sum = ______; // initialize sum // Prompt for and open the input file Scanner user = new Scanner( System.in ); System.out.print("File name? "); String fileName = user.next().trim(); Scanner scan = new Scanner( new File(fileName) ); // Note how this is done. // get the number of integers to add up limit = scan.nextInt(); int count = ______; // initialize count while ( count <= ______ ) { value = scan.nextInt(); sum = ______; // add to the sum count = ______; // increment count } System.out.println( "Grand Total: " + sum ); } }
Why is user input trimmed? user.next().trim()