Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:10:23+00:00 2026-06-15T01:10:23+00:00

Will you check my Method and let me know what I’m doing wrong? thank

  • 0

Will you check my Method and let me know what I’m doing wrong? thank you 🙂

public static void sortByVehicleMakeModel(Vehicle[] vehicles) {
    boolean swapped = true;

    for(int y = 0; y < vehicles.length && swapped; y++) {
        swapped=false;
        for(int x = 0; x < vehicles.length - (y+1); x++) {
            if(vehicles[x].getMake() && vehicles[x].getModel().compareTo(vehicles[x + 1].getMake() && vehicles[x].getModel())) {    
                swap(vehicles, x, x + 1);
                swapped=true;
            }
        }
    }
}

my error is on the second statement .compareto()
The operator && is undefined for the argument type(s) java.lang.String, java.lang.String

However, this code works just fine:

public static void sortByOwnerName(Vehicle[] vehicles) {
    boolean swapped = true;

    for(int y = 0; y < vehicles.length && swapped; y++) {
        swapped=false;
        for(int x = 0; x < vehicles.length - (y + 1); x++) {
            if(vehicles[x].getOwner().getName().compareTo(vehicles[x + 1].getOwner().getName())> 0) {   
                swap(vehicles, x, x + 1);
                swapped=true;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:10:24+00:00Added an answer on June 15, 2026 at 1:10 am

    I would suggest adding a int getCost() to the Vehicle object and then using something like vehicles[x].getCost() > vehicles[x - 1].getCost() for your if statement.

    Also, this sort is not very efficient. Maybe Vehicle should implement Comparable and use Collections.sort() to sort.


    Just read the update to your question.

    Try this:

    if (vehicles[x].getMake().compareTo(vehicles[x - 1].getMake()) < 0 || 
       (vehicles[x].getMake().compareTo(vehicles[x - 1].getMake()) == 0 &&
        vehicles[x].getModel().compareTo(vehicles[x - 1].getModel()) < 0)) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop a method which will check a users input, and
first let me say I know the following code will be considered bad practices..
I'm working on a script that will check if a user is an Administrator
I'm writing an .htaccess file that will check if a requested page exists in
i have this code that will check the array contains the specific string and
I have this code that will check for if the class more-results are in
I need a function which will check for the existing URLs in a string.
I'm looking a symbol regex in PHP that will check submitted usernames and reject
I'm using C# to create simple application that will check the database repeatedly after
Need to create a custom DNS name server using C which will check against

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.