-2

I have a method that is supposed to return the smallest value of an array. The array is in the parametre of the method, so you input the values of your own choosing when you make an object of the class. This is the method I have come up with so far:

public class minsteNummer {

public minsteNummer() {
}

public int minsteNummer(Integer[] nummer) {
    int minste = 0; 

    for(int i = 0; i< nummer.length; i++){
        if(nummer[i] <= nummer.length) {
            minste = i;
            System.out.println("Minste nummer er " + minste);
        } else if(nummer.length == 0) {
            return 0;
        }
    }
    return 0;
} 
}

It does not execute the way I want it to, and I cant figure out what exacly it prints, but it is definetly not the smalles number of the array. I have tried with a while loop, but that does not work either.

Does anyone know where the fault in the code is, and how to improve it? I would also like it to just return, not print, the smalles number, but when I try to put "return minste;" in the if-statement, it says "unexpected return value".

Thanks in advance.

r0mi1
  • 17
  • 4

3 Answers3

2

There are few places in your code that need attention:

  • As method scope is public you should always check for invalid input

  • Should not assign: int minste = 0; as there could be a negative number in a given array

  • When assign minimum number, should always compare it to the loop current number

    if (minste > nummer[i]) minste = nummer[i];

  • Finally always return your minimum number return minste;

All together:

public static int minsteNummer(Integer[] nummer) {
  if (nummer==null || nummer.length == 0)  {
    throw new IllegalArgumentException("Bad or empty array");
  }
  int minste = nummer[0]; 

  for (int i = 1; i< nummer.length; i++){
    if (minste > nummer[i]) minste = nummer[i];
  }
  System.out.println("Minste nummer er " + minste);
  return minste;
}

It is worth to mention that you could use Java build-in functionality for such a basic task, i.e. sort array in ascending order and get first element:

public static int minsteNummer(Integer[] nummer) {
  if (nummer==null || nummer.length == 0)  {
    throw new IllegalArgumentException("Bad or empty array");
  }
  Arrays.sort(nummer);
  return nummer[0];
}  
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • I'll just add that the sorting solution, while cleaner, is less efficient. Whether it is merge sort or quick sort (see [Why java Arrays use two different sort algorithms for different types?](http://stackoverflow.com/questions/3707190/why-java-arrays-use-two-different-sort-algorithms-for-different-types)), iterating through the array once is definitely the more efficient way to go. – Justin Hellreich Mar 03 '17 at 17:15
  • @JustinHellreich Completely agree, sorting array is less efficient then just one pass over it. But it was just an example to inquire more interest in Java `Collections` topic – MaxZoom Mar 03 '17 at 17:41
1

use streams

Integer[] arrayB = null;
OptionalInt min = Arrays.stream(arrayB).mapToInt(Integer::intValue).min();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
public int minsteNummer(Integer[] nummer) {
  int minste = Integer.MAX_VALUE; 

  for(int i = 0; i< nummer.length; i++){
    if(nummer[i] < minste ) {
        minste = nummer[i] ;
  }
  if(minste != Integer.MAX_VALUE)
       return minste;
  else
       return 0;
} 
ronce96
  • 84
  • 2
  • 9