I am trying to display the elements of my ArrayList<LibraryItem>
in an action listener method by calling a printlibrary
method from another class. I keep getting:
The method printLibrary(ArrayList<LibraryItem>) in the type Library is not applicable for the arguments.
I don't understand what I am doing wrong could you please help me.
import java.util.ArrayList;
public class Library {
private ArrayList<LibraryItem> items;
public Library() {
items = new ArrayList<LibraryItem>();
}
public void addItem(LibraryItem newItem) {
items.add(newItem);
}
public LibraryItem searchForItem (String name) {
for(LibraryItem searchForItem: items) {
if(searchForItem.getName().equals(name))
return searchForItem;
}
return null;
}
public static void printLibrary(ArrayList<LibraryItem> items) {
for(int i = 0; i < items.size(); i++) {
System.out.println(items.get(i));
}
}
}
public void actionPerformed(ActionEvent ev) {
String t = title.getText();
int y = Integer.parseInt(year.getText());
int q = Integer.parseInt(quantity.getText());
library.addItem(new LibraryItem(t,y,q));
library.printLibrary(); -------->>>> ERROR HERE!!!
} this is the edited version and the error public void printLibrary(ArrayList<LibraryItem> items) {
for(int i = 0; i < items.size(); i++) {
System.out.println(items.get(i));
}
}
} public void actionPerformed(ActionEvent ev) {
String t = title.getText();
int y = Integer.parseInt(year.getText());
int q = Integer.parseInt(quantity.getText());
library.addItem(new LibraryItem(t,y,q));
library.printLibrary();
} this is my code for libraryitem class ` public class LibraryItem
{
private String name;
private int year, quantity;
LibraryItem(String nameIn, int yearIn, int quantityIn)
{
name = nameIn;
year = yearIn;
quantity = quantityIn;
}
public boolean rent()
{
if(quantity > 0)
{
quantity--;
}
return true;
}
public String toString()
{
return name + " " +"has"+ " " +quantity+ "books on loan";
}
public String getName()
{
return name;
}
public int getQuantity()
{
return quantity;
}
public int getYear()
{
return year;
}
}
`
this is my code for library ` import java.util.ArrayList;
public class Library {
private ArrayList<LibraryItem> items;
public Library()
{
items = new ArrayList<LibraryItem>();
}
public void addItem(LibraryItem newItem)
{
items.add(newItem);
}
public LibraryItem searchForItem (String name)
{
for(LibraryItem searchForItem: items)
{
if(searchForItem.getName().equals(name) )
return searchForItem;
}
return null;
}
public void printLibrary()
{
for(int i = 0; i< items.size(); i++)
{
System.out.println(items.get(i));
}
}
}
`
Look at your call:
library.printLibrary();
Now look at the declaration:
public static void printLibrary(ArrayList<LibraryItem>items)
This is a static method, with a parameter which is a list. You're trying to call it as an instance method (which Java allows even when calling a static method, unfortunately, but which should be avoided anyway) but not supplying any arguments.
Given that Library
already has a list of items, I suggest you just change the declaration to make it a parameterless instance method:
public void printLibrary()
... leaving the body of the method the same, which will then pick up the instance variable items
.
See more on this question at Stackoverflow