Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Find the lowest number

1. ## Find the lowest number

I have to find the average, highest, and lowest numbers in an array. I already have the average and highest, and I thought I could find the lowest number the same way I found the highest, but it's not working. It just keeps coming out to 0. Please help, I'm pretty confused and I've tried out different ways, but I want to see if there are better ways than doing MAX_VALUE for the lowest, then looping it through.
```import java.util.Scanner;

public class Test_Scores {
public static void main(String[] args) {
//array,scanner,values
Scanner input = new Scanner(System.in);
double[] allScores = new double[5];
double sum = 0;
double average = 0;
double highest = allScores[0];
double lowest = allScores[0];

for(int i = 0; i < allScores.length; i++) {
System.out.print("Enter score for test " + (i + 1) + ": ");
allScores[i] = input.nextDouble();
}
for(int a = 0; a < allScores.length; a++) {
sum += allScores[a];
average = sum / 5;
}
for(int h = 0; h < allScores.length; h++) {
if (allScores[h] > highest) {
highest = allScores[h];
}
}
for(int l = 5; l < allScores.length; l--) {
if (allScores[l] < lowest) {
lowest = allScores[l];
}
}
//output
System.out.println("The average grade is: " + average);
System.out.println("The highest grade is: " + highest);
System.out.println("The lowest grade is: " + lowest);
}
}```
These are the results
```Enter score for test 1: 98
Enter score for test 2: 96
Enter score for test 3: 92
Enter score for test 4: 65
Enter score for test 5: 85

2. ## Re: Find the lowest number

Well, initially your lowest score is set to 0. Unless you enter a number less than 0, your for loop will not find anything "lower"

Setting the highest and lowest scores should be done after you have taken the input from the user.

3. ## The Following User Says Thank You to Parranoia For This Useful Post:

Elyril (April 13th, 2014)

4. ## Re: Find the lowest number

When you are using a loop to search an array for highest or lowest, One technique is to take the first one in the array for the initial value and to compare all the others in the array against it, changing the value when found.

5. ## The Following User Says Thank You to Norm For This Useful Post:

Elyril (April 13th, 2014)