Reputation: 446
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
Upvotes: 0
Views: 1328
Reputation: 2845
Without using any boolean flags and for unlimited num# of inputs we can get +ve or -ve Max values.
public static void main(String[] args) {
int highest=Integer.MIN_VALUE, lowest=Integer.MAX_VALUE, num=0;
Scanner scan = new Scanner(System.in);
while(true){
System.out.print("Enter a number:");
String input = scan.nextLine();
if (input.equals("")) {
break;
}else{
num = Integer.parseInt(input);
if (num > highest){
highest = num;
}
if(num < lowest){
lowest = num;
}
}
}
System.out.println("Highest number is: " + highest);
}
Upvotes: 0
Reputation: 58
You could use the Object version of Integer and do a null check like this:
import java.util.Scanner;
public class FindGreatest {
public static void main(String[] args) {
Integer greatest = null;
Scanner in = new Scanner(System.in);
while (true) {
System.out.print("Input an integer: ");
input = in.nextLine();
if ("".eq(input)) {
break; // out of while true loop
}
try {
int current = Integer.parseInt(input);
if (greatest == null || current > greatest) {
greatest = current;
}
} catch (NumberFormatException e) {}
}
if (greatest != null) {
System.out.println(greatest);
}
}
}
Upvotes: 0
Reputation: 49803
The cleanest approach is to read the first value before the loop starts, and use that to initialize greatest
. You'll probably need an extra if
, to handle the case of no input values, but then you don't need to worry about that possibility once you get in the loop.
Upvotes: 1
Reputation: 1573
add :
int greatest = Integer.MIN_VALUE;
delete :
if (first) {
greatest = current;
first = false;
initialized = true;
}
else if (current > greatest) {
greatest = current;
}
add:
if(current > greatest)
greatest = current;
Upvotes: 4