# 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);
}
Asked:
###### 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.

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

is this your complete code? you are missing an opening brace in your method  displaySmallestNumber
Commented:
what is the error?

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

}
}

seems like it should just be the one?
Commented:
ah I see that double closing brace should be at the end to close your class
Commented:
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);
}
}
``````

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.

Java 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.