Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this findMin method, i am trying to return the minimum integer in the numbers ArrayList. . However, if the list is empty, it must return Integer.MIN_VALUE instead. Currently my method is giving me the errors, MIN_VALUE is undeclared and 0 is bad operand types for binary operator.

This is the class:

What I have tried:

Java
<pre>public class WrapperExample
{
    private Scanner scanner;
    private ArrayList<integer> numbers;

    /
     * Constructor for objects of class WrapperExample.
     */
    public WrapperExample()
    {
        scanner = new Scanner(System.in);
        numbers = new ArrayList<>();
    }

    /
     * Read integer from standard input (System.in)
     * and store them in the list. The end of the
     * input is indicated by a negative integer.
     * The negative integer must not be stored in the list.
     */
    public void readNumbers()
    {

        int i = scanner.nextInt();
        // Get rid of any previous numbers.<pre> numbers.clear();
        // Read a fresh set of integers ...
        while (i >= 0){
            numbers.add(i);
            i = scanner.nextInt();
        }
    }

    /**
     * Print out the integers in the numbers list.
     * Print the numbers on a single line, with a single space
     * between each.
     */
    public void printNumbers()
    {
        System.out.print (numbers);
    }

    public Integer findMin(){
        if(numbers > 0){
            return numbers.MIN_VALUE;
        }
        else{
            return Integer.MIN_VALUE;
        }
}
}
Posted
Updated 2-Dec-22 7:51am

1 solution

The minimum is the smallest number. So as you read them in, start by setting variable minimum equal to Integer.MAX_VALUE. Then as you read them in, compare each number to minimum. If it is smaller then replace minimum with the new number. at the end minimum will contain the smallest number.

Given all your questions I think some time working through Java Tutorials Learning Paths[^] would help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900