Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Can someone help me with Java Programming!!!!?

This is a very remedial problem as I am in a basic java course, but I kinda need some help on it. when I call the sort function I keep getting a message that's saying that the variable can't be found. what can i do to make this work? and please keep it simple because this is basic after all.

public class Sort

{

private static int numbers = 0;

public static void main(String[] args)

{

int[] numbers = {20,10,40,30};

numbers.sort();

}

}

1 Answer

Relevance
  • 1 decade ago

    This really confused me for a while, just finishing my first year in an Computer Science degree I'm also somewhat of a Java beginner :)

    But I found what the problem is!

    'sort' is not a method of all arrays, its a method of the 'Arrays' class. This is a separate class you need to import, you'd end up with something like this:

    import java.util.Arrays;

    public class Sort {

    public static void main(String[] args)

    {

    Sort sortObject = new Sort();

    }

    public Sort(){

    int[] numbers = {20,10,40,30};

    Arrays.sort(numbers);

    }

    }

    I also moved it into the constructor of the class instead of the main method but that's just semantics. Hope that helps :)

Still have questions? Get your answers by asking now.