Note that there are some explanatory texts on larger screens.

plurals
  1. POsorting method crashes when running
    primarykey
    data
    text
    <p>So I have been trying to run my LibraryTest.java program but it crashes when it has to use sortBooksByTitle() and sortBooksByNumPages(). The two sorting methods compile, but when I try to run the test class, it crashes. These are my three java files.</p> <p><strong>Book.java</strong></p> <pre><code>public class Book { private String author; private String title; private int numPages; public Book() { title = "EMPTY"; } public Book(String titleIn, String authorIn, int numPagesIn) { title = titleIn; author = authorIn; numPages = numPagesIn; } public String getAuthor() { return author; } public String getTitle() { return title; } public int getNumPages() { return numPages; } public String toString() { return title + " by " + author + " (" + numPages + " pages)"; } } </code></pre> <p><strong>Library.java</strong></p> <pre><code>import java.util.Random; import java.util.Arrays; public class Library { private Book[] array; private int count; private Random randomBook = new Random(); public Library(int numBooks) { array = new Book[numBooks]; count = 0; } public int getCount() { return count; } public void addBook(Book b) { //check if program can add new book if (count &lt; array.length) { array[count] = b; count++; } //if array is full, a message is thrown else { System.out.println("The Library is full!"); } } //Adds content of a library to another library. public void addLibrary(Library l) { for (Book b : l.array) { addBook(b); } } //Returns a book after receiving a String input. public Book getBook(String book) { for (int i = 0; i &lt; array.length - 1; i++) { String titleBook = array[i].getTitle(); if (titleBook.equals(book)) { return array[i]; } } Book newBook = new Book(); return newBook; } //Returns the book located in the array index given by the input. public Book getBook(int index) { if (index &lt; array.length) { System.out.printf("num: %d", index); return array[index - 1]; } Book newBook = new Book(); return newBook; } //Uses the random number generator and returns a book located in the array which //index is the random number obtained. public Book getBook() { int num = randomBook.nextInt(array.length); //System.out.printf("random num: %d", num); return array[num]; } //Sorts books alphabetically. public void sortBooksByNumPages() { for (int i = 0; i &lt; array.length - 1; i++) { for (int j = i + 1; j &lt; array.length; j++) { if (array[i].getNumPages() &gt; array[j].getNumPages()) { Book temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } //Sorts books by number of pages in ascending order. public void sortBooksByTitle() { for (int i = 0; i &lt; array.length - 1; i++) { for (int n = i; n &lt; array.length; n++) { if (array[n].getTitle().compareToIgnoreCase(array[i].getTitle()) &lt; 0) { Book temp = array[i]; array[i] = array[n]; array[n] = temp; } } } } //Output each book's information. public String toString() { String s = "Number of books: " + count + "\n"; for (int i = 0; i &lt; array.length; i++) { s = s + array[i] + "\n"; } return s; } } </code></pre> <p><strong>LibraryTest.java</strong></p> <pre><code>public class LibraryTest { public static void main(String args[]) { Library lib = new Library(3); Book b1 = new Book("Java: How to Program", "Deitel and Deitel", 1496); lib.addBook(b1); Book b2 = new Book("A Brief History of Time", "Stephen Hawking", 212); lib.addBook(b2); Book b3 = new Book("The Art of War", "Sun Tzu", 384); lib.addBook(b3); Book b4 = new Book("Ender's Game", "Orson Scott Card", 352); // This addBook call should fail since the Library lib is full lib.addBook(b4); Book b5 = new Book("The Singularity is Near", "Ray Kurzweil", 672); Library lib2 = new Library(10); lib2.addBook(b4); lib2.addBook(b5); System.out.print("\n\nOriginal library contents\n"); // This should display that there are 3 books in the library &amp; info System.out.print(lib); System.out.print("\n\nAfter combining libraries\n"); lib2.addLibrary(lib); lib = lib2; // This should display that there are 5 books in the library &amp; info System.out.print(lib); System.out.print("\n\nSorted by title\n"); try { lib.sortBooksByTitle(); } catch (Exception e) { System.out.println(e.toString()); System.out.println(e.getMessage()); } // This should display the books in alphabetical order by title System.out.print(lib); /* * System.out.print("\n\nSorted by number of pages\n"); * lib.sortBooksByNumPages(); // This should display the books in * increasing order of the number of //pages each one has * System.out.print(lib); */ // This should display Ender's Game System.out.print("\n\nBook 2:\n" + lib.getBook(1)); // This should display the EMPTY book System.out.print("\n\nBook 20:\n" + lib.getBook(20)); System.out.print("\n\nBook 'The Art of War':\n" + lib.getBook("The Art of War")); // This should randomly display a book from the library (potentially //different each time) System.out.print("\n\nRandom book:\n" + lib.getBook()); } } </code></pre> <p>Again the 3 files compile just fine but crash when I try to run. Help me please. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload