Java noob returning a number help

Associate
Joined
19 Jul 2006
Posts
1,847
Think this is a fairly easy solve but i cant do it

Code:
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Maxnumber step1;
        step1 = new Maxnumber();
        System.out.println(step1.????)
    }

}

Code:
import java.util.Arrays;

public class Maxnumber {

   int numbers[]= {1,5,-9,12,-3,89,18,23,4,-6};

    public int maxValue(int[] numbers){
        int x = 0;
	  Arrays.sort(numbers);  //sort the array into order
          x = numbers[numbers.length-1];
          return x;
// or return numbers[numbers.length-1];

	}
}

No matter what i put after the System.out.println(step1.****);
I cant get a proper number, I dont need to us a toSting thing do I as its already an int.

TIA
 
Code:
step1 = new Maxnumber();

In your maxnumber class there are no instance variables. Therefore where you invoke the constructor maxnumber(), which you haven't coded specifically, step1 will equal zero/nothing/null.

Code:
public class Maxnumber {

   private int[] numbers; // This is your instance variable

   
   public Maxnumber()
   {
      this.numbers = new int[] { 1,5,-9,12,-3,89,18,23,4,-6 };
   }

    public int maxValue(int[] numbers){
        int x = 0;
	  Arrays.sort(numbers);  //sort the array into order
          x = numbers[numbers.length-1];
          return x;
// or return numbers[numbers.length-1];

	}
}

Now when you invoke Maxnumber(), step1 will have the variable" numbers".

You will need an accessor method to get the value of this variable as it is private.

Code:
public int[] getNumbers()
{
    return ( this.numbers );
}

Now in your main method:

Code:
public static void main(String[] args)
{
    // TODO code application logic here
    // This will create a new Maxnumber() object with an instance variable "numbers". This is a list of integers {1,5,-9,12,-3,89,18,23,4,-6}.
    Maxnumber step1 = new Maxnumber(); 
    
    // Note this will only print out the memory location of the list. You need a seperate method for individual numbers.
    System.out.println( step1.getNumbers() );
    
    // This will print out the max number in the list.
    System.out.println( step1.maxValue() );
}

Hopefully I didn't make any mistakes. Good luck!
 
Last edited by a moderator:
Thanks Garee but i get lots of red lines with that code :(

Code:
public Maxnumber()
   {
      this.numbers = {1,5,-9,12,-3,89,18,23,4,-6};
   }

Think this maybe the culprit illegal start of expression?
 
Code:
public Maxnumber()
   {
      this.numbers = new int[] { 1,5,-9,12,-3,89,18,23,4,-6 };
   }


Sorry i'm doing this without a compiler. It should be that ^

Edit: Ignore my maxValue() method aswell, it's wrong.

Fixed it:

Code:
   public int maxValue()
   {
        int[] sorted = ( this.numbers );
        Arrays.sort( sorted );
        
        return ( sorted[ sorted.length - 1 ] );
   }

Also, to print your list you can create a seperate method.

Code:
   public String printNumbers()
   {
	   String output = "";
	   
	   for ( int number : this.numbers )
	   {
		   output += number + " ";
	   }
	   
	   return ( output );
   }

Then in your main you have:

step1.printNumbers();
 
Last edited by a moderator:
Back
Top Bottom