0

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;
        }
    }
3
  • 1
    You have a bug in the max method - you're stopping one number too soon, and effectively disregarding the last number on the list. Commented Jan 22, 2015 at 3:45
  • and it starts at index 1, so you're leaving out the first number also. That said, your program "does nothing" because you don't use the result of the function in your main() Commented Jan 22, 2015 at 4:04
  • @Gus No, he/she isn't missing out the first number, because the initial value of currMax is m[0]. Commented Jan 22, 2015 at 4:16

5 Answers 5

3

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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...

Comments

0

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);

Comments

0
  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;
        }
    }

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.