I'm making a Library class that's supposed to be able to store book objects from a book class, and I seem to be running into a problem at the for-each loop.
Main class:
package library;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
{
Scanner k = new Scanner(System.in);
String[] subject = { "Russian", "Literature" };
Book X = new Book("Misanthrope", "Penguin Co.", "Uncle Vanya, Rakolnikov", subject, 10213, 1835, true);
Library Shelf = new Library();
Shelf.add(X);
System.out.println("Enter Subject you'd like to search"); //Prompt for Subject Search method
String G = k.next();
Shelf.SearchBySubject(G);
}
}
}
Library Class:
p
ackage library;
import java.util.ArrayList;
public class Library {
private ArrayList list = new ArrayList(); //Declare Private ArrayLists
private ArrayList ResultList = new ArrayList();
private String B[];
public void add(Book Y) //add method for adding books into
{ //Array list
list.add(Y);
}
public void SearchBySubject(String G) //Method so Search By Subject
{ //Using Array lists and Foreach
for(Book z: list){
B = z.GetSubject();
if(B.contains(G))
{
ResultList.add(z);
}
}
for(Book X: ResultList)
{
System.out.println("Title: " + X.GetTitle() + " Authors: " + X.GetAuthor() +
" ID Number: " + X.GetNum());
}
}
}
Book Class:
package library;
public class Book extends Library {
private String Title, Publisher, Authors;
private String[] Subject;
private int CatalogNumber, Year;
private boolean Circulating;
// Book Constructor
public Book(String title, String publisher, String authors, String[] subject, int catnum, int year, boolean circ)
{
Title = title;
Publisher = publisher;
Authors = authors;
Subject = subject;
CatalogNumber = catnum;
Year = year;
Circulating = circ;
}
//Series for Get Methods
public String[] GetSubject() //Get Subject String
{
return Subject;
}
public String GetTitle() //Get Title String
{
return Title;
}
public int GetNum() //Get CatalogNumber Integer
{
return CatalogNumber;
}
public String GetAuthor() // Get Authors String
{
return Authors;
}
}
The specific error I'm receiving is:
"incompatible types
required: library.Book
found: java.lang.Object"
The 2 specific lines with problems are:
for(Book z: list){
for(Book X: ResultList)
Any help you could provide would be very appreciated. Been stuck on it for awhile, and tried browsing forums to find the information but had no luck.
Thanks!