# smallest element

Page 1 of 1

## 2 Replies - 4090 Views - Last Post: 22 October 2007 - 02:36 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=35825&amp;s=ef1cd1c8866477952dcebd53034e6e40&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 m_milam

Reputation: 2
• Posts: 28
• Joined: 15-September 07

# smallest element

Posted 21 October 2007 - 06:40 PM

I am supposed to find the smallest element in an array of integers. I was asked to use {1, 2, 4, 5, 10, 100, 2, -22} as the integers to test this against. Here is my code. I do not get any errors but I do not get any results either. I have a feeling I am close.

```/**
* Class:  BMIS 3301 Web Programming with Java
* Instructor: Jingshan Xiao
* Description: Reversing Numbers : Exercise 6_3
* Due: 10/14/2007
* @author Matthew Milam
*/
import javax.swing.JOptionPane;

public class Exercise6_9f{
// Main method
public static void main(String[] args) {
int[] numbers = {1, 2, 4, 5, 10, 100, 2, -22};

}

public static int smllElement(int[] numbers) {
int max = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if ( max > numbers[i] )
max = numbers[i];
}

//Prepare Result
String output = "The array is ";
for (int i = 0; i < numbers.length; i++) {
output += numbers[i] +"";
}

output +="\nThe Smallest Element is " + max;

//Display Result
JOptionPane.showMessageDialog(null, output);
return max;
}
}

```

Is This A Good Question/Topic? 0

## Replies To: smallest element

### #2 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: smallest element

Posted 21 October 2007 - 09:20 PM

You are very close, the only thing you are forgetting is to call the method from inside your main method. I'm not sure why you need your method to return a value, but currently it does.

```  public static void main(String[] args) {
int[] numbers = {1, 2, 4, 5, 10, 100, 2, -22};
// need to call the method from main
int maxNum = smllElement(numbers);

}
```

Good job!

### #3 m_milam

Reputation: 2
• Posts: 28
• Joined: 15-September 07

## Re: smallest element

Posted 22 October 2007 - 02:36 AM

jayman9, on 21 Oct, 2007 - 09:20 PM, said:

You are very close, the only thing you are forgetting is to call the method from inside your main method. I'm not sure why you need your method to return a value, but currently it does.

```  public static void main(String[] args) {
int[] numbers = {1, 2, 4, 5, 10, 100, 2, -22};
// need to call the method from main
int maxNum = smllElement(numbers);

}
```

Good job!

Thanks for the insight. Whenever I am stuck I am usually a line or two off.