Categories
Development

error with returning a value from a method in java

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.

Leave a Reply

Your email address will not be published. Required fields are marked *