0

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

4
  • 3
    Pick the smallest value available - Integer.MIN_VALUE. Any value they enter must either be greater than or equal to that. Get rid of all the pointless boolean flags... Commented Oct 11, 2014 at 11:11
  • @Boris the Spider Good idea, thanks. The booleans are pretty disgusting, yeah. Commented Oct 11, 2014 at 11:18
  • If you don't want to use a list then use an array or if not add all the user inputs to a string with each input seperated by a delimiter and use split satement to get the inputs and parse the each time into an integer in a loop and sort the in ascending order and take the last value of the String using substring and that will be your highest value Commented Oct 11, 2014 at 11:24
  • @rert588: I think the point is not to store all of the values in any way; as you can see from the answers provided, doing so is pretty straight-forward. Commented Oct 11, 2014 at 11:27

4 Answers 4

4

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Most logical solution, but not very programmer-y (read: complex)
@WeinerBoat: I guess it depends on how good a programmer one wants to be :P
0

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);
        }
    }
}

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.