0

enter image description hereOnce I enter in the list. it will not display the maximum. I tried to call in max in the last println it still did not work.

 ArrayList<Double> numbers = new ArrayList<Double>();

      Scanner keyboard = new Scanner(System.in);
      System.out.println("Please enter a list of numbers: ");

      while (keyboard.hasNextDouble())
      {
         double input = keyboard.nextDouble();
         numbers.add(input);      
      }
    Double max = Collections.max(numbers);
        System.out.println("The Maximum is: "  );

}} 
12
  • You're not terminating the loop, enter something that's non double value like string. Commented Apr 21, 2016 at 2:30
  • Im not understanding. Commented Apr 21, 2016 at 2:45
  • if you keep entering a double value like 3.14, the loop will go on reading the input. There's no termination condition in the loop, like read only 10 times then break. In your case however it's not an infinite loop as keyboard.hasNextDouble() returns false if the input value is not a valid double string. So you can break it by typing something like "break me", "kill me", "quit" anything that's not a double value. Commented Apr 21, 2016 at 2:47
  • okay. And I will go about that how? Commented Apr 21, 2016 at 2:50
  • Check the answer from SkyWalker, in that case if you enter -99 then loop will break and System.out.println will be executed, also check answer from Scary Wombat, you haven't appended the max to your print statement, right now it's just printing The Maximum is: Commented Apr 21, 2016 at 2:53

3 Answers 3

1

How about

Edit

// check to make sure that numbers has some elements

if (numbers.size () <= 0) {
   // some message
   return;
}
Double max = Collections.max(numbers);
System.out.println("The Maximum is: "  + max );
//                                     ^^^^^^
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't see that coming, completely missed it.
I have attached a picture of my results I am getting
@TB2017 You need to enter at least one number. You can't get the maximum of zero numbers.
0
      while (keyboard.hasNextDouble())
      {
         double input = keyboard.nextDouble();
         numbers.add(input);  
         if(input == -99)  break;
      }

Break will help you.

Full Code with example:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ArrayList<Double> numbers = new ArrayList<Double>();

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter a list of numbers: ");

        while (keyboard.hasNextDouble()) {
            double input = keyboard.nextDouble();
            numbers.add(input);
            if (input == -99)
                break;
        }
        Double max = Collections.max(numbers);
        System.out.println("The Maximum is: " + max); // you have missed to add max here
    }
}

Output:

Please enter a list of numbers: 
2
3
13
4
-99
The Maximum is: 13.0

3 Comments

I tried your idea. I am still getting the same results as the picture I posted.
wait nevermind. my question says maximum of a set of positive numbers. You may assume that all elements in the set will be positive. So I cant use -99
@TB2017 you are violating with keyboard.hasNextDouble(). It only takes input that are number. So you need to specify the terminating number. Or you can tell how much number you have. Then you will make a counter. When counter number exceeds your given number,then you will give break. Hope it will help you
0

From the javadocs: Collections.max throws:

NoSuchElementException - if the collection is empty.

Your list is empty.

Comments

Your Answer

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