# help with find the lowest number in a array.

Page 1 of 1

## 2 Replies - 899 Views - Last Post: 03 September 2013 - 03:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=328113&amp;s=74558c9414c7d11a4f6bf5d0edd2b83d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cherrypepsi2013

Reputation: 0
• Posts: 4
• Joined: 28-March 13

# help with find the lowest number in a array.

Posted 01 September 2013 - 07:34 PM

The user enters x amount of numbers into an array.
I can not get it to print out the lowest number. It shows the last number the user enters as the lowest number.
example:

enter how many numbers. 5
count 0 1
count 1 2
count 2 5
count 3 3
count 4 4
1
2
5
3
4
total is 15
average is 3
high number is 5
low number is 4

```
import java.util.Scanner;

public class array2
{
public static void main (String args[])
{
Scanner input = new Scanner (System.in);

int number=0;
int x= 0 ;
int total = 0;
int a;
int sum = 0;
int average = 0;

number = input.nextInt();

int array [] = new int[number];

System.out.printf("Enter the  %d  numbers now.\n", number);
for (int i = 0; i < array.length; i++)
{
array[i] = input.nextInt();
}
System.out.println ("Your number in the array\n");
printArray(array);
// find the sum
for (int i : array)
{
sum +=i;
}
System.out.printf ("Total is %d: \n ", sum);
// find the average
average = sum/array.length;
System.out.printf ("Average is %d: \n ",average);

//  find the high number
int high = array[0];
for(int i = 1 ; i < array.length; i++)
{
if(high < array [i])
{
high = array[i];
}
}
System.out.printf("The high number is %d \n",high);
//  find the low number
int low = array[0];
for (int i = 0 ; i < array.length; i++)
{
if (low  > array[i]);
{
low = array[i];
}
}
System.out.printf("The low number is %d \n",low);
}
public static void printArray(int array[])
{
int total = 0;
int i;
int n = array.length;
for ( i = 0; i < n; i++)
{
System.out.printf("Count %d %d \n",i, array[i]);
}
}
}

```

*Edited: fixed code tags

This post has been edited by pbl: 01 September 2013 - 07:36 PM

Is This A Good Question/Topic? 0

## Replies To: help with find the lowest number in a array.

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: help with find the lowest number in a array.

Posted 01 September 2013 - 07:39 PM

Fixed code tags but code indentation is still horrible

```  int min = array[0];
for(int i = 1; i < array.length; ++i) {
if(array[i] < min)
min = array[i];

```

should do the job

### #3 The Arcanist

Reputation: 5
• Posts: 37
• Joined: 27-August 13

## Re: help with find the lowest number in a array.

Posted 03 September 2013 - 03:52 AM

It won't give the correct output because of the following code you made :
```for (int i = 0 ; i < array.length; i++)
{
if (low  > array[i]);
{
low = array[i];
}

```

Their is a semicolon after the if statement which shouldn't be there.

Other than that, I would recommend you to follow the conventions and start the class name with a capital letter, remove few variables which remain unused throughout your code and use proper indentation to make your code more readable.
Cheers.