I created a Java class of a booklist.
public class bookList {
public String title, author, genre;
public bookList (String beginTitle, String beginAuthor, String beginGenre)
{
title = beginTitle;
author = beginAuthor;
genre = beginGenre;
}
public String getTitle(){return title;}
public String getAuthor(){return author;}
public String getGenre(){ return genre;}
public void setTitle(String titleUpdate){title = titleUpdate;}
public void setAuthor(String authorUpdate){author = authorUpdate;}
public void setPubDate(String genreUpdate){genre = genreUpdate;}
@Override
public String toString() {
return "title: " + title + '\t'+
"author: " + author + '\t'+
"genre: " + genre +
"\n";
}
}
Then I created a driver class, adding books to a toRead ArrayList.
import java.util.*;
public class Main {
public static void main(String[] args) {
int userResponse = 1;
String title, author, genre;
String userInput;
Scanner scan = new Scanner(System.in);
bookList newBook = new bookList(" ", " ", " ");
ArrayList<bookList> toRead = new ArrayList<>();
bookList example1 = new bookList("Mesopotamia", "Gwendolyn Leick", "history");
bookList example2 = new bookList("A Week to be Wicked", "Tessa Dare", "romance");
bookList example3 = new bookList("Say Yes to the Marquess", "Tessa Dare", "romance");
bookList example4 = new bookList("Istanbul", "Thomas F. Madden", "history");
bookList example5 = new bookList("Circe", "Madeline Miller", "fiction");
bookList example6 = new bookList("The Song of Achilles", "Madeline Miller", "fiction");
bookList example7 = new bookList("A Lady by Midnight", "Tessa Dare", "romance");
bookList example8 = new bookList("When a Scot Ties the Knot", "Tessa Dare", "romance");
bookList example9 = new bookList("Worlds of Medieval Europe", "Clifford Backman", "history");
bookList example10 = new bookList("The Sumerians", "Samuel Noah Kramer", "history");
Collections.addAll(toRead, example1, example2, example3, example4, example5, example6, example7, example8, example9, example10);
System.out.println("Welcome to your to read list!");
System.out.println("To see your reading list, enter 1,");
System.out.println("To sort your reading list by title, type 2");
userResponse = scan.nextInt();
if (userResponse == 1) {
System.out.println(toRead);
}
if (userResponse == 2) {
Collections.sort(toRead);
}
}
The Collections.sort(toRead); is underlined in red and it says "no instance(s) of type variable(s) T exist so that bookList conforms to Comparable<? super T>"
I've tried Google and other questions, but they're not helping. Any assistance is appreciated. Thank you!
(And before anyone asks, yes, this is homework. We're supposed to design and create a program. We didn't talk about sorting Array Lists in class).
As pointed out in the comments, you're not providing your program a way to sort "bookList". should it sort it based on tilte? author? something else? java isn't gonna just assume what is the natural order of "bookList".
You must tell your program to sort based on specific criteria such as title, author, or any other data field in the class.
One way to do this is by having the "bookList" class implement the Comparable interface and override the compareTo() method
}