Errors in textpad

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Txpaduser
Posts: 1
Joined: Sat Nov 08, 2003 10:48 pm

Errors in textpad

Post by Txpaduser »

Hello,

I've been trying to create a horoscope java program, and in order to do so I have to use a sentinel loop. Can anyone give me any help with that, as I am really stuck.

Thank you.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

I don't understand the Topic: "Errors in textpad"? That sounds like a bug, but that is not what your question is about. Perhaps change the subject to something like "Need help with sentinel loops"?

Many programs with loops that read one or more data items and process them. Often you don't know how many data items will be processed.
One traditional way is to signify the end of the input data with a sentinel value. This last value does not count as input data and usually has a value outside of the range of permissible values for the data item. For example, use -1 as sentinel for age data items. The loop controlled condition can be formulated with the sentinel value.

Here is an example:

Code: Select all

final int sentinel = -1;  // final  indicates this integer value is constant and can't be changed. 
int score; 
int count=0; 
int minScore = 101; // minScore will be replaced by the smallest score entered so far 
int maxScore = 0;    // maxScore will be replaced by the largest score entered so far 
double sum = 0.0; 
score  = Console.readInt("Enter the score or -1 to stop"); 
while (score != sentinel) { 
    sum += score; 
    if (score > maxScore) 
        maxScore = score; 
    if (score < minScore) 
        minScore = score; 
    count++; 
   score  = Console.readInt("Enter the score or -1 to stop"); 
} 
Console.message("The number of students="+count+ 
                               ", avg score="+sum/count); 
Console.message("The minimum score is "+minScore+ 
                               "; the maximum score is"+maxScore+".");
If processing records from a data file of unknown length, then EOF is frequently used as a sentinel value.
Hope this was helpful.............good luck,
Bob
Post Reply