0

I am trying to call the sort method in my main and I cannot figure out how to do it correctly.

public static void sortVehicles(Vehicle[] vehicles)
{
    Arrays.sort(vehicles);
    for(int i = 0; i < vehicles.length; i++)
    {
        System.out.println(vehicles[i]);
    }
    return;
}

I was doing this.....

public class 
{
    public static void main(String[] args) throws Exception {
        Vehicle[] vehicles = fillArray(args[0]);

        vehicles = fillArray(args[1]);

        System.out.print("Enter 1 to print all of the files.\nEnter 2 to sort the files by email and print.\nEnter 3 to print the amount of files.\nEnter 4 to print only bike and truck files.\nEnter 5 to print only area codes (987).\nEnter number: ");

        Scanner input = new Scanner(System.in);

        int x = input.nextInt();

        while (true) {
            if (x == 1) {
                printAll(vehicles);
            } else if (x == 2) {
                sortVehicles(vehicles);
            } else if (x == 3) {
                printRecordNumber(vehicles);
            } else if (x == 4) {
                printBicyclesAndTrucks(vehicles);
            } else if (x == 5) {
                printByAreaCode(vehicles);
            }
        }

    }// end main

It gives me an error that says it does not recognize the symbol of the sortVehicle

5
  • 3
    Where is the sortVehicles method declared? Commented Oct 18, 2013 at 20:35
  • 5
    Your class doesn't have a name. Commented Oct 18, 2013 at 20:35
  • Consider using an IDE. Commented Oct 18, 2013 at 20:36
  • I'd recommend using a Try Catch block instead of having your main throw an exception, as well Commented Oct 18, 2013 at 20:40
  • I got it working thanks! I was calling it from the class it was declared. Commented Oct 18, 2013 at 20:47

3 Answers 3

4

If these two functions are not in the same class (or the class in which sortVehicles is declared is not a superclass of the one where this main method is declared), you will need to specify the name of the class in which sortVehicles is declared when calling the sortVehicles method, thusly:

VehicleSorter.sortVehicles(vehicles)

Alternatively, you may statically import the sortVehicles method in the class with your main method:

import static com.example.VehicleSorter.sortVehicles;
Sign up to request clarification or add additional context in comments.

Comments

0

Alternatively, you can make main in your Vehicle class.

public class Vehicle
{
    public static void main(String[] args) throws Exception {
        Vehicle[] vehicles = fillArray(args[0]);

    vehicles = fillArray(args[1]);

    System.out.print("Enter 1 to print all of the files.\nEnter 2 to sort the files by email and print.\nEnter 3 to print the amount of files.\nEnter 4 to print only bike and truck files.\nEnter 5 to print only area codes (987).\nEnter number: ");

    Scanner input = new Scanner(System.in);

    int x = input.nextInt();

    while (true) {
        if (x == 1) {
            printAll(vehicles);
        } else if (x == 2) {
            sortVehicles(vehicles);
        } else if (x == 3) {
            printRecordNumber(vehicles);
        } else if (x == 4) {
            printBicyclesAndTrucks(vehicles);
        } else if (x == 5) {
            printByAreaCode(vehicles);
        }
    }

}// end main

public static void sortVehicles(Vehicle[] vehicles)
{
    Arrays.sort(vehicles);
    for(int i = 0; i < vehicles.length; i++)
    {
        System.out.println(vehicles[i]);
    }
    return;
}

}

1 Comment

sortVehicles is static, therefore in this case creation of Vehicle is reduntant.
0
  • static methods should be called with <class name>.<function name> e.g. Math.max(int , int).

So here in your case it should be:

<Class name to which sortVehicles belongs>.sortVehicles(vehicles)

  • else put the static method inside the class to which main method belongs:

    public class { public static void main(String[] args) throws Exception { Vehicle[] vehicles = fillArray(args[0]);

        vehicles = fillArray(args[1]);
    
        System.out.print("Enter 1 to print all of the files.\nEnter 2 to sort the files by email and print.\nEnter 3 to print the amount of files.\nEnter 4 to print only bike and truck files.\nEnter 5 to print only area codes (987).\nEnter number: ");
    
        Scanner input = new Scanner(System.in);
    
        int x = input.nextInt();
    
        while (true) {
            if (x == 1) {
                printAll(vehicles);
            } else if (x == 2) {
                sortVehicles(vehicles);
            } else if (x == 3) {
                printRecordNumber(vehicles);
            } else if (x == 4) {
                printBicyclesAndTrucks(vehicles);
            } else if (x == 5) {
                printByAreaCode(vehicles);
            }
        }
    
    }
    public static void sortVehicles(Vehicle[] vehicles)
    {
        Arrays.sort(vehicles);
        for(int i = 0; i < vehicles.length; i++)
        {
             System.out.println(vehicles[i]);
        }
        return;
    }
    
    }
    

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.