0

hello in the code below I am sorting (sort method) a small array to find the largest number. I then print the answer in the (display method).

But to extend my knowledge I want to mass the max value back to them main in a return statement and then print from there....simply to learn how to return a value.

package christmas;

public class maxvalue 
{ 
    public static void main(String[] args) 
    { 
        int[] data={10,90,30}; 
        sort(data); 
        System.out.println("\nmax number is :"); 
        display(data); 
        System.out.println(data);
    } 
    static int display(int num[]) 
    { 
        System.out.print(num[0] + " "); 
        return num[0];
    }
    static void sort(int num[]) 
    { 
        int i, j, temp; 
        for(i=0; i<num.length-i;i++) 
        {  
            for(j=0; j<num.length-i-1;j++) 
            { 
                if(num[j]<num[j+1]) 
                { 
                    temp = num[j]; 
                    num[j] = num[j+1]; 
                    num[j+1] = temp; 
                }
            }
        }
    }
}

the output is:

max number is : 90 [I@4617c264

90 is the max value as printed by the display value. But after this I have a return of the max value and then I try and print the return. But instead of an integer it looks like a memory location is being printed.

Any ideas please - I am student but this is not homework - simply trying to catch up. I appreciate that there are more elegant ways to calculate the max value in an array but what I am trying to learn is the passing of arguments to and from a method.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
  • 1
    You're printing the array. Are you saying you *don't* want to print the array, just the maximum value? Because as for "returning a value from a method", you're already doing that in `display`, so I'm not sure what you're asking. – khelwood Dec 26 '19 at 21:38
  • 1
    Just delete `System.out.println(data);` from the `main`. If you want to know what is printed it is elaborated in [this question[(https://stackoverflow.com/questions/4712139/why-does-the-default-object-tostring-include-the-hashcode). – Turing85 Dec 26 '19 at 21:39

4 Answers4

0

The reason is that you are trying in your last System.out to print data, which is an array, and that's the reason why you see a memory address.Try printing display(data) and you will see as the output the desired number.

Cipri
  • 69
  • 1
  • 8
0

Instead of printing the returned value, you are printing the data array memory location:

System.out.println(data);

You should change that line with:

System.out.println(display(data));

With that line we have:

  1. Your display method is called and it prints the max value
  2. Your display method returns the max value
  3. println takes that returned value and prints it
PajLe
  • 791
  • 1
  • 7
  • 21
0

try System.out.println(data[0]); data is your array therefore printing data without an index will only print its memory location

0
private static int sort(int[] array){
  int a, b, max = 0;
  for (int i = 1;//If i was 0, it would have thrown an ArrayIndexOutOfBoundsException.
      i < array.length; i++){
    a = array[i-1];//The first element in the array.
    b = array[i];//The second one, and so on.
    if (a > b) max = a;//Check the bigger number.
    else max = b;
  }
  return max;
}
private static void display(int nr){
  System.out.print(nr);//Or what you want to do with the number.
}
public static void main(String[] args){
  int[] data={10,90,30};
  display(sort(data));
  //Or do it like this.
  int max = sort(data);
  display(max);
}
Ev0lv3zz
  • 58
  • 8