See below.
Whenever there is a sum, be sure that it is initialized to zero. Whenever there is a count, be sure that it is initialized (usually to zero or one) and check for off-by-one problems.
import java.util.Scanner;
class AddUpFile
{
public static void main ( String[] args )
{
Scanner scan = new Scanner( System.in );
int value;
int sum = 0 ; // initialize sum
int count = 1 ; // initialize count
while ( count <= 100 )
{
System.out.print("Enter a number: ") ;
value = scan.nextInt() ; // get next integer
sum = sum + value; // add to the sum
count = count + 1 ; // increment count
}
System.out.println( "Grand Total: " + sum );
}
}
If you run this program as is you must enter 100 integers. This could be tedious. For testing purposes change "100" to a smaller number.
If you don't want to require exactly 100 numbers in the file you could write the program as a sentinel terminated loop. Perhaps end looping when a sentinel value of -999 is read in. But the danger is that -999 might sometimes be legitimate data.
Another (better) way to deal with different amounts of input data is to use the
hasNextInt() method of Scanner. See the next chapter.
Say that you do have a file of 100 integers and run the program by doing this:
C:\java AddUpFile < largeData.txt
What will the user see on the monitor?