0

Hello Everyone I was instructed to do the following but came up with 10 errors which are listed below can anyone please help with this.

Write a sentinel loop that will sum all positive integer values entered; use a negative number to end the loop. Be sure to test for valid data entry; if data entered is incorrect give the user and error message and have them re-enter the value until they enter in an integer.
Assume: console is a Scanner object that was initialized to the keyboard.

public class positiveinteger {

   public static void main(String[] args) {

      int inputNumber;   
      int sum;           
      int count;         

      sum = 0;
      count = 0;

      TextIO.put("Enter your first positive integer: ");
      inputNumber = TextIO.getlnInt();

      while (inputNumber != 0) {
         sum += inputNumber;   
         count++;              
         TextIO.put("Enter your next positive integer, or 0 to end: ");
         inputNumber = TextIO.getlnInt();
      }

      /* Display the result. */

      if (count == 0) {
         TextIO.putln("You didn't enter any data!");
      }
      else {
         average = ((double)sum) / count;
         TextIO.putln();
         TextIO.putln("You entered " + count + " positive integers.");
         TextIO.putf("Their average is %1.3f.\n", average);
      }

   } // end main()

} // end class positiveinteger

ERROR CODES:

location: class positiveinteger
         TextIO.putln("You didn't enter any data!");
         ^
positiveinteger.java:28: cannot find symbol
symbol  : variable average
location: class positiveinteger
         average = ((double)sum) / count;
         ^
positiveinteger.java:29: cannot find symbol
symbol  : variable TextIO
location: class positiveinteger
         TextIO.putln();
         ^
positiveinteger.java:30: cannot find symbol
symbol  : variable TextIO
location: class positiveinteger
         TextIO.putln("You entered " + count + " positive integers.");
         ^
positiveinteger.java:31: cannot find symbol
symbol  : variable average
location: class positiveinteger
         TextIO.putf("Their average is %1.3f.\n", average);
                                                  ^
positiveinteger.java:31: cannot find symbol
symbol  : variable TextIO
location: class positiveinteger
         TextIO.putf("Their average is %1.3f.\n", average);

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by kramerd
0

The first error says you are using a variable called average which you haven't declared. The rest of the errors have to do with the TextIO class, probably because you're missing an import statement.

If you post again, please put your code inside CODE tags so it gets properly indented and has line numbers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.