Hi all,
I have to declare an array of LibraryBook objects and sort
them either by title, author or page count, as the user
requests.

I've got the basics down but I'm missing something.

public class LibraryBookSort
{
  public static void main(String[] args)
  {
    LibraryBook[] myBook = new LibraryBook[5];
	 	
	 myBook[0] = new LibraryBook("Java Programming", "Joyce Farrell", 881);
	 myBook[1] = new LibraryBook("A Book", "E Author", 345);
	 myBook[2] = new LibraryBook("B Book", "F Author", 450);
	 myBook[3] = new LibraryBook("C Book", "G Author", 650);
	 myBook[4] = new LibraryBook("D Book", "H Author", 750);    
    
//below I was just checking if it worked to this point.  Its not compiling.  
	 for (int x = 0; x < myBook.length; ++x)
           myBook[x].setTitle();
	 for (int x = 0; x < myBook.length; ++x)
           System.out.println(myBook[x].getTitle());
  }

      // Sort the names alphabetically using a bubble sort and display the results.
         
      //   System.out.print("Ascending sort: ");
      //   display(title);
  //}
   	// ----------------------------------------------------
   	// Displays the names in the String.  
   	// ----------------------------------------------------
      public static void display(String[] title)
      {
         for (int x=0; x < title.length; ++x)
         {
            System.out.println(myBook[x] + " ");
         }
         System.out.println();
      }
   
   
   	// ----------------------------------------------------
   	// Sort the names alphabetically (ascending)
   	// ----------------------------------------------------
      public static void titleSort(String[] title)
      {
         for (int x=0; x < title.length - 1; ++x)
            for (int y=0; y < title.length - 1; ++y)
            {
               if (title[y].compareTo(title[y+1]) > 0)
               {
                  String temp = title[y];
                  title[y] = title[y+1];
                  title[y+1] = temp;
               }
            }
      }
 }
import java.util.*;
import javax.swing.*;
public class LibraryBook
{
  private String title;
  private String author;
  private int pageCount;
  
  //contructor
  public LibraryBook(String bookTitle, String bookAuthor, int bookpageCount)
  {  
	 title = bookTitle;
	 author = bookAuthor;
	 pageCount = bookPageCount;
  }
  
  
  //get methods
  public String getTitle()
  {
    return title;
  }
  public String getAuthor()
  {
    return author;
  }
  public int getPageCount()
  {
    return pageCount;
  }
  //set methods
  public void setTitle(String bookTitle)
  {
    title = bookTitle;
  }
  public void setAuthor(String bookAuthor)
  {
    author = bookAuthor;
  }
  public void setPageCount(int bookPageCount)
  {
    pageCount = bookPageCount;
  }

  
}

And what's the problem?

I'm stuck, its not compiling and I can't figure why. After I get it to compile I'll try to add sort methods and make them work but I'm really just spinning my wheels.

My code is a mess, I think my brain is tired of looking at it and I can’t figure out what works and what doesn’t so I can move on. Take a look and see if there is anything you notice or any help you can provide! Thanks!

The problem is:

a. Create a class named LibraryBook that contains fields to hold methods for setting and getting a LibraryBook’s title, author, and page count. Save the file as LibraryBook.java
b. Write an application that instantiates five LibraryBook objects and prompts the user for values for the data fields. Then prompt the user to enter which field the LibraryBooks should be sorted by-title, author, or page count. Perform the requested sort procedure and display the LibraryBook objects. Save the file as LibraryBookSort.java.

LibraryBook class and LibraryBookSort (page 362):
• Declare an array of LibraryBook objects and sort
them either by title, author or page count, as the user
requests. See pages 328‐333 for code that can be
used in sorting objects. Please include the Java
textbook as one of the LibraryBook objects

If you post the error messages that the compiler is giving you, that'll help a lot - usually the compiler output will point you directly to the problem.

As a rule, it's good to assume that people who can help you are likely to be busy, and you should try to make it easy for them to help you. That means telling them exactly what the problem is. "It doesn't work" doesn't give me anything to work with. "When I compile, it reports an ExceptionalExceptionException at line 455, see the attached stack trace" tells me exactly what the problem is and where to look for it.

Read over Eric Raymond's piece (linked in the sticky thread at the top of the forum) for more useful tips on getting useful help.

Sorry, I guess I figured the code is there and its easier to copy and paste it into Eclipse and see the compile error and the code in action. Since I'm a beginner to Java and thats what I do to test code, why would it be any different for others? haha I'll give it another try a little later, too bad I can't edit my original post to be more clear.

As you like. Maybe someone's got time to build your project and debug it for you. That ain't me, though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.