3

I got my main task to work (rough translation):

Write a method, which returns the smallest number from a 1d array of integers.

Now i have to do a side task (rough translation):

Add an if statement to check if the array is null or empty, and if it is, return 0.

method:

public static int smallestNumber (int[] array1) {
 int smallest = array1[0];
 if (array1 == null || array1.length == -1) {
 return 0;
 }
 else {
 for (int element : array1) {
 if (element < smallest) {
 smallest = element;
 }
 }
 }
return smallest; 
}

main:

public static void main(String[] args) {
 int[] tab1 = {}; //empty array?
 int smallestNumber = smallestNumber(tab1);
 System.out.printf("The smallest number is: %d", smallestNumber);
}

The method works if i only check for null. But i'm confused why it wont work on empty array.

int[] tab1 = {};

EDIT: I also tried with array1.length == 0;

DrSkull
772 silver badges11 bronze badges
asked Mar 31, 2019 at 18:02
1
  • Hi Captain, Why are you initializing an int[] with curly brackets? and not using new, for example int[] intArray = new int[]; Commented Mar 31, 2019 at 18:27

3 Answers 3

3

Firstly, Arrays are of non-negative size so array1.length cannot be -1, instead make the comparison with 0.

Secondly, the assignment int smallest = array1[0]; tries to access 0th position of an empty array which will result in java.lang.ArrayIndexOutOfBoundsException.

So in conclusion move the assignment to smallest in the else block and check the condition for empty or null array before you try to access any array value.

public static int smallestNumber (int[] array1) {
 int smallest;
 if (array1 == null || array1.length == 0) {
 return 0;
 }
 else {
 smallest = array1[0];
 for (int element : array1) {
 if (element < smallest) {
 smallest = element;
 }
 }
 }
 return smallest;
}
answered Mar 31, 2019 at 18:04
3
  • 1
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 this is what i get when i do == 0 Commented Mar 31, 2019 at 18:05
  • move the assignment for smallest below the condition Commented Mar 31, 2019 at 18:07
  • @CaptainApple now try this code, the line where you assigned the initial value of smallest was incorrect move it inside the else block and also change the comparison to -1. Commented Mar 31, 2019 at 18:11
1

Try checking for null first, as it is safer to check for that first. You also can use the method isEmpty() for the length.

public static int smallestNumber (int[] array1) {
if (array1 == null) {
 return 0;
}
if (array1.isEmpty()) {
 return 0;
}
 int smallest = array1[0];
 for (int element : array1) {
 if (element < smallest) {
 smallest = element;
 }
 }
 return smallest;
}

or add this as an alt:

public static int smallestNumber (int[] array1) {
if (array1 != null) {
 if (array1.isEmpty()) {
 return 0;
 }
 int smallest = array1[0];
 for (int element : array1) {
 if (element < smallest) {
 smallest = element;
 }
 }
 return smallest;
 } else {
 return 0;
 }
}
answered Mar 31, 2019 at 18:13
0
1
public static int smallestNumber (int[] array1) {
 if (array1 == null || array1.length == 0) {
 return 0;
 }
 int smallest = array1[0];
 for (int element : array1) {
 if (element < smallest) {
 smallest = element;
 }
 }
 return smallest; 
}
answered Mar 31, 2019 at 18:07
2
  • Thank you, yes wasnt sure if it was true or not but i went with == 0 first before becoming desperate to even try -1. Issue solved above Commented Mar 31, 2019 at 18:10
  • I updated my answer. First we need check array1 == null || array1.length == 0 before array operation, ex: array1[0]. Commented Mar 31, 2019 at 18:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.