Question

For an array of books, the assignment is to get attributes of the objects from the user, then sort based on their choice of author, title, or page count. My bubble sort is working for the page count but not when the user chooses author or title, so I'm guessing I'm missing something with reference storage vs. value storage, maybe? Thanks!

import javax.swing.*;
import java.util.*;
public class LibraryBookSort {

public static void main(String[] args) {
    LibraryBook[] books = new LibraryBook[5];
    LibraryBook tempBook = new LibraryBook();
    String enteredAuthor = "", enteredTitle = "";
    String enteredPageCount;
    String sortBy;
    String displayString = "";
    int parsedSortBy;
    int parsedPageCount;
    int booksLength = books.length;

    // populate the array
    for (int x = 0; x < booksLength; ++x)
        books[x] = new LibraryBook();

    // get property values from user
    for (int x = 0; x < booksLength; ++x)
    {
        enteredTitle = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's title:");
        books[x].setTitle(enteredTitle);
        enteredAuthor = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's author:");
        books[x].setAuthor(enteredAuthor);
        enteredPageCount = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's page count:");
        parsedPageCount = Integer.parseInt(enteredPageCount);
        books[x].setPageCount(parsedPageCount);
    }

    // sort by property values
    sortBy = JOptionPane.showInputDialog("Choose option to sort by: (1) title, (2) author, or (3) page count");
    parsedSortBy = Integer.parseInt(sortBy);

    while (parsedSortBy < 1 || parsedSortBy > 3)
    {
        sortBy = JOptionPane.showInputDialog("Invalid selection, please choose option to sort by: (1) title, (2) author, or (3) page count");
        parsedSortBy = Integer.parseInt(sortBy);
    }

    if (parsedSortBy == 1)
    {
        for (int a = 0; a < booksLength - 1; ++a)
        {
            for (int b = 0; b < booksLength - 1; ++b)
            {
                if (books[b].getTitle().compareTo(books[b+1].getTitle()) > 1)
                {
                    tempBook = books[b];
                    books[b] = books[b+1];
                    books[b+1] = tempBook;
                }
            }
        }
    }
    else if (parsedSortBy == 2)
    {
        for (int a = 0; a < booksLength - 1; ++a)
        {
            for (int b = 0; b < booksLength - 1; ++b)
            {
                if (books[b].getAuthor().compareTo(books[b+1].getAuthor()) > 1)
                {
                    tempBook = books[b];
                    books[b] = books[b+1];
                    books[b+1] = tempBook;
                }
            }
        }
    }
    else
    {
        for (int a = 0; a < booksLength - 1; ++a)
        {
            for (int b = 0; b < booksLength - 1; ++b)
            {   
                if (books[b].getPageCount() > books[b+1].getPageCount())
                {
                    tempBook = books[b];
                    books[b] = books[b+1];
                    books[b+1] = tempBook;
                }
            }
        }
    }

    for (int i = 0; i < booksLength; ++i)
    {
        displayString += (books[i].getTitle() + ", by " + books[i].getAuthor() + ". " + books[i].getPageCount() + " pages.\n");
    }
    JOptionPane.showMessageDialog(null, "Books sorted by your choice:\n\n" + displayString);
}

}

Était-ce utile?

La solution

The comparison should be

.... > 0

Not

.... > 1

The contract of compareTo is to return zero on equal and something >0 for "larger" and <0 for "smaller". In practice the values are -1, 0 and +1.

Autres conseils

Since you are using compareTo method to sort, you must know that compareTo depends on lexicographic ordering.

The oracle docs quote as following, This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value: this.length()-anotherString.length()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top