Addition Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        
12 +7

Answer:

Java will throw an InputMismatchException and your program will end.

Addition Program

Say that you have a file containing 100 integers, one integer per line. You wish to add up all these integers. You can write a program to add numbers from keyboard input. Once it has been tested and debugged, it can be used with the input file. Here is a start on the program:

import java.util.Scanner;

class AddUpFile
{
  public static void main ( String[] args )  
  {
    Scanner scan = new Scanner( System.in );
    int value;
    int sum = ; // initialize sum

    int count = ; // initialize count
    while ( count  100 )
    {
      System.out.print("Enter a number: ");
      value  = scan.nextInt();
      sum    = ; // add to the sum
      count  = ; // increment count
    }

    System.out.println( "Grand Total: " + sum );
  }
}

 

QUESTION 10:

Fill in the blanks.