user123429
user123429

Reputation: 187

Beginner Java - looping through an array

So this class is supposed to print out the largest number in the array defined under the main method (aka 22) however when I run it nothing happens. I'm sure this is a very stupid question but today is my first day with java and I have already spent an embarrassing amount of time trying to figure it out. Thanks!

  public class fun {
        public static void main (String [] args) {
        int[] numbers = new int[] {9, 2, 15, 2, 22, 10, 6};
        max(numbers);
        }

        public static int max(int[] m) {
            int length = m.length;
            int counter = 1;
            int currMax = m[0];
            while (counter <= (length - 2)){
                if (m[counter] > currMax){
                currMax = m[counter];
                }
                counter = counter + 1;
            }
            return currMax;
        }
    }

Upvotes: 0

Views: 101

Answers (5)

Yui
Yui

Reputation: 74

The iterration itself could work properly if you would iterrate through the entire Array. You are missing the last index of your Array. So it won't compare the Array[on_last_index] with the currentMax value anymore.

To solve this simple problem all you need to do is to change the condition in the while loop.

while(counter <= length-1) { ... }

So let me try to explain it to you. If you create an Array of follow elements inside it:

numbers = new int[]{9, 2, 15, 2, 22, 10, 6};

then every single value in your array got it's own index. For instance if you want to get the first element of your array, then all you have to do is get the element of the array on index 0. 0 because it is the first element that you want to pick => index 0 is the first index for the first element in your array. Now at your example you got an array of the size 7 => you got 7 elements inside your array. But remember how to get elements of your array? Right! You can get elements by their indizes. The first element got the index 0, the secound 1,...,the last 6. 6 becuase we started at 0, so if you want to iterrate threw an array then always from 0 to array.length-1.

Upvotes: 0

Nimesh
Nimesh

Reputation: 802

  public class fun {
        public static void main (String [] args) {
        int[] numbers = new int[] {9, 2, 15, 2, 22, 10, 6};
        int maxNum = max(numbers); //change this
System.out.println(maxNum);
        }

        public static int max(int[] m) {
            int length = m.length;
            int counter = 1;
            int currMax = m[0];
            while (counter <= (length - 1)){ //change this
                if (m[counter] > currMax){
                currMax = m[counter];
                }
                counter = counter + 1;
            }
            return currMax;
        }
    }

Upvotes: 0

Shailendr singh
Shailendr singh

Reputation: 686

In your main method get the return value from max method in one variable then print it

 public static void main (String [] args) {
    int[] numbers = new int[] {9, 2, 15, 2, 22, 10, 6};
   int val = max(numbers);
   System.out.println(val);
    }

Bingo...

Upvotes: 1

EDToaster
EDToaster

Reputation: 3180

You have to print out the number as in

System.out.println(max(numbers));

Alternatively you can assign it to an int variable and then do stuff with it:

int maxNumber = max(numbers);

Upvotes: 0

kirbyquerby
kirbyquerby

Reputation: 725

Your max method does indeed return the max number, but you're not doing anything with that number.

Perhaps you wish to print out the max?

System.out.println(max(numbers));

Upvotes: 3

Related Questions