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 8731565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:16:04+00:00 2026-06-13T09:16:04+00:00

This code asks user to enter vehicle object that has name, model year, listing

  • 0

This code asks user to enter vehicle object that has name, model year, listing price, and percent Discount. The problem that is occurring here, when user enters all of the above info the car object is added to the bottom of the array list and not in the alphabetical order. Note the list was alphabetized before.

while (!valid) {
    String str = scan.nextLine();
    try {
        boolean found = false;
        System.out.println("Enter car name: ");
        name = scan.nextLine();
        System.out.println("Enter car model year: ");
        modelYear = scan.nextLine();
        System.out.println("Enter car list price: ");
        listPrice = scan.nextDouble();
        System.out.println("Enter car percent discount: ");
        percentDiscount = scan.nextDouble();

        int i = 0;
        loc = 0;
        while (!found && i < carList.size()) {
            String nameRetrievedFromCarList = carList.get(i).getName();
            String nameToAdd = "";
            if (nameToAdd.compareToIgnoreCase(nameRetrievedFromCarList) < 0) {
                loc++;
                found = true;

            }
            i++;

        }// end while

        Proj1CarData newCar = new Proj1CarData(name, modelYear, listPrice, percentDiscount,
                discountAmount, netPrice);
        carList.add(loc, newCar);

        valid = true;

    }// end try

    catch (NumberFormatException nfe) {
        System.out.println("Wrong entry:  Try again");
    }// end catch

}
  • 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-13T09:16:05+00:00Added an answer on June 13, 2026 at 9:16 am

    While slightly off topic, you could use Collections.binarySearch to determine where the new value should be inserted…

    From the Java Docs

    Returns:
    the index of the search key, if it is contained in the list; otherwise, (-(insertion point) – 1). The insertion point is defined as
    the point at which the key would be inserted into the list
    : the index
    of the first element greater than the key, or list.size() if all
    elements in the list are less than the specified key. Note that this
    guarantees that the return value will be >= 0 if and only if the key
    is found.

    Proj1CarData newCar = new Proj1CarData(name, modelYear, listPrice, percentDiscount, discountAmount, netPrice);
    
    int index = Collections.binarySearch(carList, newCar);
    if (index < 0) {
        index = (index * -1) - 1;
    }
    
    carList.add(index, newCar);
    

    This assumes that Proj1CarData is Comparable, other wise you will need to supply your own Comparator

    Proj1CarData newCar = new Proj1CarData(name, modelYear, listPrice, percentDiscount, discountAmount, netPrice);
    
    int index = Collections.binarySearch(carList, newCar, 
        new Comparator<Proj1CarData>() {
            public int compare(Proj1CarData car1, Proj1CarData car2) {
                return car1.getName().compareToIgnoreCase(car2.getName());
            }
        });
    
    if (index < 0) {
        index = (index * -1) - 1;
    }
    
    carList.add(index, newCar);
    

    UPDATED

    List<String> names = new ArrayList<String>(25);
    names.add("Hurzdiirn");
    names.add("Alydriira Talabdiira");
    names.add("Urlidil Sineth");
    names.add("Quavyraen Belarral");
    names.add("Belarayne'bryn Agh'Quarbryn");
    names.add("Alakgos");
    names.add("Sszoj'hrae Laelraema");
    names.add("Szornet");
    names.add("Filojafay");
    names.add("Lltril'net Chaszhrae");
    
    Collections.sort(names);
    
    for (int index = 0; index < names.size(); index++) {
        String name = names.get(index);
        System.out.println("[" + index + "] " + name);
    }
    
    int insertAt = Collections.binarySearch(names, "Luke");
    if (insertAt < 0) {
        insertAt = (insertAt * -1) - 1;
    }
    
    names.add(insertAt, "Luke");
    
    for (int index = 0; index < names.size(); index++) {
        String name = names.get(index);
        System.out.println("[" + index + "] " + name);
    }
    
    Collections.sort(names);
    for (int index = 0; index < names.size(); index++) {
        String name = names.get(index);
        System.out.println("[" + index + "] " + name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that asks the user to enter their Google Analytics code
I have code that asks the user to input a log file name so
I have this code that asks for a username and password. Just a simple
Ok, I have this code that asks an input for a username and a
I have been trying to write some code that asks the user for several
I'm currently working on a program that asks a user to enter a secret
There is a login page in aspx.net that asks the user to enter in
This code asks the user for data and subsequently a number: $ cat read.c
This code is the core of a much larger script that works great in
The exercise asks for a code which can convert the user input of the

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.