Tess Tess - 1 year ago 92
Java Question

Why does my 'if' statement in my 'why' loop not change one of my objects?

I'm brand new to Java and creating a simple program that takes information output from another program and uses it via command line for a loop. No matter how many times I look at it, I can't figure out why the 'if (minValue > value' doesn't change minValue.

Output given some information that should probably produce a minValue:

Minimum: 0 @
Maximum: 75 @ DummyDate3 DummyTime3
Average: 45.00

Is this a result of the while loop?

int minValue = 0;
int maxValue = 0;
String minValueTime = "";
String minValueDate = "";
String maxValueDate = "";
String maxValueTime = "";
int count = 0;
double average = 0;

* For as long as input is going through A date, a time, and a value
* will come through as a loop If the minimum value is less than the
* value coming through The minimum value will become the value. If the
* maximum value is less than the value, the maximum value will become
* the value.
while (input.hasNext() == true) {
String date = (input.next());
String time = (input.next());
int value = (input.nextInt());
if (minValue > value) {
minValue = value;
minValueDate = date;
minValueTime = time;
if (maxValue < value) {
maxValue = value;
maxValueDate = date;
maxValueTime = time;
average = average + value;


.printf("Count:%d%nMinimum: %d @ %s %s%nMaximum: %d @ %s %s%nAverage: %.2f%n",
count, minValue, minValueDate, minValueTime, maxValue,
maxValueDate, maxValueTime, average / count);

EDIT: I have tried Integer.MAX_VALUE and MIN_VALUE, but both of them result in the values of MAX_VALUE and MIN_VALUE, respectively.

Answer Source

The first value assigned to min is 0. If every integer read is bigger than 0, it will never change. What you actually need to do is assing max and min to the minValue and maxValue :

int minValue = Integer.MAX_VALUE;
int maxValue = Integer.MIN_VALUE;

This way, the first value read will most likely be lower than the minimum at the moment and the first value will also be higher than the current max value.

The min value can't go higher than this with those values, the max can't go lower than this too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download