Finding 'highest' & 'lowest'

I know how to find the highest and lowest of a predetermined amount of values, but I'm confused on how to do it with an indefinite amount. I know you can use arrays... but we haven't gone over those yet, so there has to be another method. Here's the simple program I wrote to enter in an indefinite amount of ages:

import java.util.*;

publicclass Ages348

{

publicstaticvoid main(String[] args)

{

/*** Below is my identification of all the identifiers (or variables)

that I will be using in the following code.***/

int ages =0;

int totalAgesAverage = 0;

int totalAgesSum = 0;

int agesInput;

Scanner scannerObject =new Scanner(System.in);

System.out.println("This program will allow you to enter in the age, in years,");

System.out.println("of an indefinite amount of people. It will then output the ");

System.out.println("number of ages that you entered, the sum of those ages, the");

System.out.println("average age entered, as well as the highest and lowest age.\n");

System.out.println("Enter -1 when you are done entering ages.\n");

System.out.println("\nEnter your ages here:");

agesInput = scannerObject.nextInt();

/***This loop is created so the user can continue to enter as mnay ages as they would like.

when they are finished, they will enter "-1" and the loop will terminate ****/

while (agesInput!= -1)/*While loop to enter. Enter -1 to exit */

{

totalAgesSum += agesInput;/*Sums all the ages entered.*/

ages ++;/* Counts the number of ages entered */

agesInput = scannerObject.nextInt();

}/*End of while loop.*/

System.out.println("\n");

totalAgesAverage = totalAgesSum / ages;/*Calculates average age.*/

/*This is where all the information is output to the DOS prompt./

System.out.println("You have entered a TOTAL of " + ages + " ages");

System.out.println("The SUM of all ages antered is: "+totalAgesSum+"");

System.out.println("The AVERAGE age is: "+totalAgesAverage+"");

System.out.println("\n\n");

}/*End of main method.*/

}/*End of class Ages348.*/

I can get the total, sum and average... but can someone point me in the right direction as to how I would find the highest and lowest numbers entered by the user?

Thanks in advance for the assistance...

[3441 byte] By [TTierno2a] at [2007-11-26 17:36:58]
# 1

It goes something like this: as the input goes along, you need to compare the value that was just entered, against the currentHighestAge. If the newly entered age is higher, replace it, else, leave it alone.

Same goes for the lowest.

You can also keep a list of all the entered numbers, and caluculate what you need afterwards...

There's a gazillion was to do this, this is just one of them.

SurfManNLa at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 2

You have variables for the sum (totalAgesSum) and number

(ages). Following this pattern you will need a couple more variables.

Inside the while loop you update the sum and the number. What you need is

some way of updating the minimum after you have read an age. Hint: if.

Updating the maximum is similar.

[Edit] Too slow...

pbrockway2a at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 3

int highest = 0;

int lowest = Integer.MAX_VALUE;

...

while (agesInput!= -1)

{

agesInput = scannerObject.nextInt();

if (agesInput > highest)

{

highest = agesInput;

}

if (agesInput < lowest)

{

lowest = agesInput

}

}

You could add your ages to a List (look at LinkedList) if you needed.

Ted.

ted_trippina at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 4

WOW.

I certainly appreciate the prompt attention.

@ ted_trippin:

I'm the kind of person who really likes to understand why code works, and not just looking to cheat.... can you confirm that I have a grasp on what's actually happening?

int highest = 0;

...

if (agesInput > highest)

{

highest = agesInput;

So to my understanding... if the 'agesInput' is higher than 0, then that value replaces the 'highest' value. It continues to repeat that expression comparing the most recent input into 'agesInput' with the currently stored 'highest' value until '-1' is entered.

int lowest = 999999;

if (agesInput < lowest)

{

lowest = agesInput

}

And then it works the same way for the lowest variable... but in reverse... if it's lower than the maximum integer (which I set to 999999 instead of "Integer.MAX_VALUE" since I'm dealing with ages), then it stores that number in the 'lowest' variable value and repeats the process until the -1.

TTierno2a at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 5
Yes.Ted.
ted_trippina at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 6

Hi everyone,

Sorry to tromp on your thread but it seems that we are doing the same project. I have pretty much the same coding as you but whenever I compile the program and it runs it always comes back with the lowest # as -1.Which is the sentinial value. How do you correct this?I have tried several different ways and nothing works.

crazyc1a at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 7
Just special case the -1. if((ageEntered < lowestAge) && (ageEntered != -1))
SomeoneElsea at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 8
For better result, set initial highest/loweast to be first input number.example: 100, 200, 500, the lowest should be 100, not Max value of Integer.
masuda1967a at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...
# 9
PS... wrong example for age 100,200, 500.10, 20, 50 are better example
masuda1967a at 2007-7-9 0:05:06 > top of Java-index,Java Essentials,New To Java...