5

Starting to think I'm using the wrong book for learning. I've copied this word-for-word from Sam's Learn Java, but the .sort(names); method is undefined for type of array.

I have a feeling it's to do with the public static void main(String[] args) { call, but I don't know how to amend it.

package arrays;

import java.util.*;

public class Arrays {
    public static void main(String[] args) {
        String names[] = { "Lauren", "Audrina", "Heidi", "Whitney",
                "Stephanie", "Spencer", "Lisa", "Brody", "Frankie", "Holly",
                "Jordan", "Brian", "Jason" };
        System.out.println("The original order:");
        for (int i = 0; i < names.length; i++) {
            System.out.print(i + ": " + names[i] + " ");
        }
        Arrays.sort(names);
        System.out.println("\nThe new order:");
        for (int i = 0; i < names.length; i++) {
            System.out.print(i + ": " + names[i] + " ");
        }
        System.out.println();
    }
}
7
  • 20
    You shouldn't name your class Arrays then, or at least qualify the second java.util.Arrays. Commented Sep 8, 2014 at 13:03
  • 1
    What Thomas says (+1). Pro tip: to print your array contents, you're better off with Arrays.toString(names) instead of indexed iteration. Commented Sep 8, 2014 at 13:06
  • 1
    Thanks all. Didn't even occur to me :\ @Mena thanks! I'm literally right at the beginning of figuring all this out, I was just hoping to run an example code and to see it work. Unfortunately my own ignorance with Java made me innocently use an inappropriate class name. Commented Sep 8, 2014 at 13:08
  • 1
    @Mena I disagree with the "pro-tip"; using toString means you get whatever the toString implementation gives you, uncustomized, without extra data that might be needed, with the brackets, etc. String.join if you're on JDK 8, possibly, or a util lib, but even that doesn't give you the ability to access the index. Commented Sep 8, 2014 at 13:08
  • 3
    @T.J.Crowder what you gave is an answer, I only gave a hint ;-) Commented Sep 8, 2014 at 13:09

2 Answers 2

11

(Thomas pointed to the answer, but as it was a comment you can't accept it; here's a CW you can accept.)

Arrays is a class in the java.util package, which has a sort method. By calling your own class Arrays, you hide the java.util one from your code (this is sometimes called "shadowing"), so the Arrays.sort(names); line is trying to use a sort method on your class (and it doesn't have one).

To fix it, you have three options:

  1. Change the name of your class (ArrayTest, whatever), or

  2. Change your call to sort to: java.util.Arrays.sort(names); (so the compiler knows you're talking about java.util.Arrays, rather than your class), or

  3. Use import static java.util.Arrays.sort; to import the method sort rather than the class Arrays and invoke it with sort(names); (without a preceding class name).

Sign up to request clarification or add additional context in comments.

Comments

-2
package again.data;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Osama
 */
public class AgainData {

    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);


        System.out.print(" Enter tour Element = ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("Enter your  number  = " + i + "  =   ");
            arr[i] = sc.nextInt();
        }
        System.out.print("sort number trtub tsoduan : ");
        for (int i = 0; i <arr.length; i++) {
            for (int j =i+1; j <arr.length; j++) {
                if(arr[i]>arr[j]){
                    int temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
              
            }
        }



        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");

        } // print sort number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.