I need to find the sum, count of, number of evens, number of odds, largest, smallest, and average of the numbers of the scanned file. I've done everything except largest/smallest. When the program runs, the largest/smallest equals the count instead of producing the largest/smallest value the count encounters.
EDIT: I understand that largest/smallest are being compared to count. I now understand that mistake. I don't understand how to find the smallest/largest value that count encounters.
Here is what I've done: NOTE: All code before while loop was prewritten by my professor and that code cannot be tampered with or altered in any way. Not allowed to use arrays either.
int count=0,sum=0, largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE, evens=0, odds=0;
double average=0.0;
while (infile.hasNext())
{
count += 1;
sum += infile.nextInt();
average = sum/count;
if (count > largest)
largest = count;
if (count < smallest)
smallest = count;
if (sum%2 != 0)
odds++;
else
evens++;
}
infile.close();
You need to compare the values being read with the previous maximum and minimum values. So, store your values into a variable and use that for the comparison/summation operations, like so:
A quick summary of the changes made:
nextInt
each time the value is needed.I still have no idea what you mean by " I don't understand how to find the smallest/largest value that count encounters." What DOES count have to do with it at all?