Sorry if the same question exists already, I couldn't find an actual answer.
I'd like to figure out a way to find the greatest integer among user-inputted values without storing all of the numbers into an array and also allowing negative numbers. The problem I've been having is that I can't initialize the 'greatest' value before first asking for one value. Here's what I have so far:
import java.util.Scanner;
public class FindGreatest {
public static void main(String[] args) {
int greatest;
int current;
boolean first = true;
boolean keepGoing = true;
boolean initialized = false;
Scanner in = new Scanner(System.in);
while (keepGoing) {
System.out.print("Input an integer: ");
String input = in.nextLine();
if (input.equals("")) {
keepGoing = false;
}
else {
try {
current = Integer.parseInt(input);
if (first) {
greatest = current;
first = false;
initialized = true;
}
else if (current > greatest) { // compiler error
greatest = current;
}
}
catch (NumberFormatException e) {
//
}
}
}
if (initialized) {
System.out.println(greatest); // compiler error
}
}
}
The last print statement isn't working out even though out it looks to me there should be no way greatest could be uninitialized at that point.
If anybody can spot my error or offer a cleaner solution it'd be nice
EDIT: Initializing 'greatest' with a random value is also apparently enough to fix this particular snippet
Integer.MIN_VALUE. Any value they enter must either be greater than or equal to that. Get rid of all the pointlessbooleanflags...