How do I display the smallest number in a collection?

This is my code:

import java.util.ArrayList;


public class Numbers {
    private ArrayList<Double> numbers;

    public Numbers() {
        numbers = new ArrayList<Double>();
    }

    /**
     *Add a number to the collection in a client specified position or at the end of the collection
     */
    public void addNumber(double number)
    {
        numbers.add(number);
    }

    /**
     * Display all numbers in the collection
     */
    public void displayNumbers()
    {
        int number = 0;

        while (number < numbers.size()) {
            System.out.println(numbers.get(number));
            number++;
        }
    }

    /**
     * Display all numbers in the collection organized in 2 categories, first the positive numbers, second the
     *negative numbers
     */
    public void displayNumbersInCategories()
    {
        System.out.println("Positive Numbers");

        int number = 0;

        while (number < numbers.size()) {
            if (numbers.get(number) >= 0.0) {
                System.out.println(numbers.get(number));
            }

            number++;
        }

        System.out.println("Negative Numbers");

        for (Double aNumber : numbers) {
            if (aNumber < 0.0) {
                System.out.println(aNumber);
            }
        }
    }

    /**
    * Display the sum of all numbers in the collection
    */
    public void displaySum()
    {
        double sum = 0.0;
        int length = this.numbers.size();
        int counter = 0;

        while (counter < length) {
            sum = sum + this.numbers.get(counter);
            counter++;
        }

        System.out.println(sum);
    }

    /**
    * Display 2 sums: the sum of all positive numbers in the collection and the sum of all negative numbers in
    * the collection
    */
    public void displaySumsInCategories()
    {
        double sum = 0.0;
        int length = this.numbers.size();
        int counter = 0;
        System.out.println("Positive Numbers");

        while (counter < length)
        {
            sum = sum + this.numbers.get(counter);
            counter++;
        }
       

        System.out.println(sum);
        //}
        System.out.println("Negative Numbers");

        for (Double aSum : numbers)
        {
            if (aSum < 0.0)
            {
                System.out.println(aSum);
            }
        }
    }


/**
 * Display the average of all the positive numbers
 */

public void displayPositiveAvg()
{
  double sum = 0.0;
  int count = 0;
  for(Double number : numbers)
  {
    if(number > 0.0)
    {
      sum += number;
      count++;
    }
  }

  System.out.println("Average is " + (sum / count));
}    
}

 
/**
* Display the smallest number in the collection
*/

public void displaySmallestNumber()

Double smallest = Double.MAX_VALUE;
        for (Double aNumber : numbers)
        {
            if (aNumber < smallest)
            {  
                smallest = aNumber;
            }
        }
System.out.println("value" + smallest);
}
ryanbecker24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ryanbecker24Author Commented:
I keep getting a error at public void displaySmallestNumber()
ryanbecker24Author Commented:
Can some please help me?
rockiroadsCommented:
what error you getting?

is this your complete code? you are missing an opening brace in your method  displaySmallestNumber
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Gurvinder Pal SinghCommented:
what is the error?

there is a left brace missing after the method declaration.
rockiroadsCommented:
before that method also I see two closing braces

}    
}

seems like it should just be the one?
rockiroadsCommented:
ah I see that double closing brace should be at the end to close your class
rockiroadsCommented:
like this

what ide r u using? eg eclipse you can do ctrl-shift-f and it tidies the code up for you

i.e.
import java.util.ArrayList;

public class Numbers {
	private ArrayList<Double> numbers;

	public Numbers() {
		numbers = new ArrayList<Double>();
	}

	/**
	 *Add a number to the collection in a client specified position or at the
	 * end of the collection
	 */
	public void addNumber(double number) {
		numbers.add(number);
	}

	/**
	 * Display all numbers in the collection
	 */
	public void displayNumbers() {
		int number = 0;

		while (number < numbers.size()) {
			System.out.println(numbers.get(number));
			number++;
		}
	}

	/**
	 * Display all numbers in the collection organized in 2 categories, first
	 * the positive numbers, second the negative numbers
	 */
	public void displayNumbersInCategories() {
		System.out.println("Positive Numbers");

		int number = 0;

		while (number < numbers.size()) {
			if (numbers.get(number) >= 0.0) {
				System.out.println(numbers.get(number));
			}

			number++;
		}

		System.out.println("Negative Numbers");

		for (Double aNumber : numbers) {
			if (aNumber < 0.0) {
				System.out.println(aNumber);
			}
		}
	}

	/**
	 * Display the sum of all numbers in the collection
	 */
	public void displaySum() {
		double sum = 0.0;
		int length = this.numbers.size();
		int counter = 0;

		while (counter < length) {
			sum = sum + this.numbers.get(counter);
			counter++;
		}

		System.out.println(sum);
	}

	/**
	 * Display 2 sums: the sum of all positive numbers in the collection and the
	 * sum of all negative numbers in the collection
	 */
	public void displaySumsInCategories() {
		double sum = 0.0;
		int length = this.numbers.size();
		int counter = 0;
		System.out.println("Positive Numbers");

		while (counter < length) {
			sum = sum + this.numbers.get(counter);
			counter++;
		}

		System.out.println(sum);
		// }
		System.out.println("Negative Numbers");

		for (Double aSum : numbers) {
			if (aSum < 0.0) {
				System.out.println(aSum);
			}
		}
	}

	/**
	 * Display the average of all the positive numbers
	 */

	public void displayPositiveAvg() {
		double sum = 0.0;
		int count = 0;
		for (Double number : numbers) {
			if (number > 0.0) {
				sum += number;
				count++;
			}
		}

		System.out.println("Average is " + (sum / count));
	}

	/**
	 * Display the smallest number in the collection
	 */

	public void displaySmallestNumber() {

		Double smallest = Double.MAX_VALUE;
		for (Double aNumber : numbers) {
			if (aNumber < smallest) {
				smallest = aNumber;
			}
		}
		System.out.println("value" + smallest);
	}
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick BarryJava DeveloperCommented:
you can use the same approach as you would with an array

http://helpdesk.objects.com.au/java/how-do-i-find-the-smallest-value-in-an-array
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.