bsc Logo

Let's get started with a input/output program!

  1. Open Eclipse
  2. Right-Click on your edu.buffalostate.cis425.fa19.exercises. package
  3. Choose: File -> New -> class
  4. In the Name: textbox type: InputOutput
  5. Click: Finish
  6. Copy the InputOutput.java Template (Java) code (below)
  7. Paste that code into your new InputOutput class in the editor
  8. Change both occurrences of put-your-lastname-here
  9. Replace the blank on the
    Scanner
    line with:
    Scanner(System.in)
  10. After the
    // add a line here to output a message advising the user to enter 9999 to exit
    comment line add:
    System.out.println( "Enter '9999' to exit" ); // informs user how to exit
  11. Replace the blank after the
    if (num == 9999) {
    line with:
    break;
  12. Replace the blank on the
    if ()
    line with:
    cnt > 0
  13. Replace the blank on the
    ave =
    line with:
    sum / (double) cnt;
  14. Replace the blank after the
    } else {
    line with:
    ave = 0;
  15. Click the Run icon

InputOutput.java Template (Java)

package edu.buffalostate.cis425.fa19.exercises.put-your-lastname-here;

/**
 * File: InputOutput.java
 *
 * Write a program that accepts 10 integers from the keyboard and
 * computes their average.
 * If the user enters 9999, the program exits early, and outputs
 * the average of the numbers entered so far.
 *
 * Fill in the blanks to make the program work 
 *
 */
/**
 *
 * @author put-your-name-here 
 *
 */

import java.util.Scanner;

public class InputOutput {
  
  public static void main( String args[] ) {
    double ave = 0, cnt = 0, num = 0, sum = 0;
    Scanner sc = new _____________;
    System.out.println("Integer Averaging Program"); 
    System.out.println("Enter 10 Integers");
    // add a line here to output a message advising the user to enter 9999 to exit
    ______________ // prompt the user for up to 10 integers
    for ( int i = 1; i < 11; i++ ) {
      System.out.print("Enter Integer " + i + ": ");
      // check if the user entered a non-integer
      if (!sc.hasNextInt()) {
        // System.err will print the message in RED
        System.err.println("Sorry, please enter an integer");
        sc.nextLine();
        i--;
        continue;
      }
      num = sc.nextInt();
      // break out of the loop if the exit code (9999) is entered
      if (num == 9999) { 
        __________
      }
      cnt++; 
      sum = (double) sum + (double) num;
    }
    // check to make sure the count is not set to zero since you can not divide by zero
    if (__________ ) { 
      ave = _____________;
    } else { 
      // if the count is zero then the program sets the average equal to zero
      ____________;
    }
    System.out.println("The average of " 
        + cnt 
        + " numbers is "
        + ave );
    sc.close();
  }
}

InputOutput Class Output

Integer Averaging Program
Enter 10 Integers
Enter 9999 to exit
Enter Integer 1: k
Sorry, please enter an integer
Enter Integer 1: 55
Enter Integer 2: 66
Enter Integer 3: 77
Enter Integer 4: 88
Enter Integer 5: 9999
The average of 4.0 numbers is 71.5