1

I'm at the stage in Java learning where, I've gone through basic tutorials, etc, but I need to be stepping up the complexity of my coding. Although the codewars 1st lesson seems borderline beyond me :(

I'm not particularly looking for the answer (cheating), but perhaps someone can critique the (very little) that I've done, and point me in the right direction. Here's the question:

Write a function that can return the smallest value of an array or the index of that value. The function's 2nd parameter will tell whether it should return the value or the index.

Assume the first parameter will always be an array filled with at least 1 number and no duplicates. Assume the second parameter will be a string holding one of two values: 'value' and 'index'.

Here's my initial code...

public class Arrays {

  public static int findSmallest( final int[] numbers, final String toReturn ) {
    //TODO: Add solution here
    int smallest = numbers[0];
    for (int i = 0; i < numbers.length; i++)
    {             
        if (numbers[i] <= smallest)
        {
            smallest = numbers[i];
        }
    }
    return smallest;
    System.out.println(smallest);

    }
}

Error:

/Arrays.java:14: error: unreachable statement System.out.println(smallest); ^ /Arrays.java:16: error: missing return statement } ^ 2 errors

I've been bashing my head about the overall logic I've been using to make steps forward with this challenge. Can anyone point out the error of my ways? Thanks for reading a long-winded description of an otherwise, I'm sure, simple problem....

*edit - please note I've not even began to deal with the first part of the challenge, which is making the distinction between returning the value or the place in the array.

mdgsec
  • 83
  • 1
  • 13

3 Answers3

2

You should remove the System.out.println(smallest); after the return statement, since it can never be reached (or put it prior to the return statement).

public static int findSmallest( final int[] numbers, final String toReturn ) 
{
    int smallest = numbers[0];
    for (int i = 0; i < numbers.length; i++) {             
        if (numbers[i] <= smallest) {
            smallest = numbers[i];
        }
    }
    System.out.println(smallest); 
    return smallest;
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    Also `i` variable can start from `1` instead of `0`, although it is not an error. – Petar Minchev Jun 21 '15 at 09:37
  • Thanks, I put that in to see what value I was ascertaining to be smallest. Makes sense now to be before the return! Can you see any obvious flaws in the logic of the code? – mdgsec Jun 21 '15 at 09:38
  • 1
    @user3127448 Well, you can take Petar's suggestion and let i iterate from 1. – Eran Jun 21 '15 at 09:39
  • 1
    And also `if (numbers[i] < smallest)`, since there are no duplicates. – Petar Minchev Jun 21 '15 at 09:41
  • 1
    Petar and @Eran, thanks a bunch! Incredibly, the part about returning an index or a value (which I thought might be difficult) turned out to be incredibly easy with another if statement. Thanks! – mdgsec Jun 21 '15 at 10:00
0

Have you considered doing this in a more functional way with Stream.min?

My Java is a little rusty but I believe you should be able to do something like this:

int smallest = numbers.stream().min((n1, n2) -> Integer.compare(n1, n2)).get();

One line of code that converts your int array to a stream then runs the min comparator on it to return the smallest number.

cjnevin
  • 311
  • 2
  • 8
0

Although Eran's answer cover everything, I include this code for the second parameter thing. :)

Note that, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called.

And How to compare strings in Java.

Here is the code:

public class Arrays {
    public static int findSmallest(final int[] numbers, final String toReturn) {
        // TODO: Add solution here
        int smallest = numbers[0];
        int index = 0;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] < smallest) {
                smallest = numbers[i];
                index = i;
            }
        }
        if (toReturn.equals("value")) {
            return smallest;
        } else if (toReturn.equals("index")) {
            return index;
        } else {
            return smallest; // default returns value
        }

    }

    public static void main(String args[]) {
        int[] numbers = {1,2,3,-1,0,22,-99};
        String toReturn = "value";
        int result = findSmallest(numbers, toReturn);
        System.out.println("The " + toReturn + " is " + result);
        toReturn = "index";
        result = findSmallest(numbers, toReturn);
        System.out.println("The " + toReturn + " is " + result);
    }
}
Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41