So the issue is I've created an object array that takes user input and stores the object. I want to remove objects using a method that finds them via their id number and removes them from the array. Honestly, I can't figure out why I can't get away from the errors. (I know my code is wrong, I just don't understand what is wrong per se. The book object is in a separate file, but I can post that as well if needed. Thank you.
import java.util.Scanner;
public class Inventory {
private int size;
private Book[] inventory;
private Scanner scan = new Scanner(System.in);
public void addBook() { //open method addBook
System.out.print("Enter the book id number: ");
int id = scan.nextInt();
for (int i = 0; i < inventory.length; i++) {
for (int j = i+1; j <inventory.length; j++)
if (inventory[i].equals(inventory[j])) {
System.out.print("This book is already in the inventory" );
}
}
System.out.print("Enter book title: ");
String title = scan.nextLine();
System.out.print("Enter book price: ");
double price = scan.nextDouble();
Book book = new Book(id, title, price);
inventory[size++] = book;
} //Close method addBook
public void removeBook() { //open method removeBook
System.out.print("Enter the book id of the book you want to remove: ");
int id = scan.nextInt();
for(int i = 0; i < inventory.length; i++) {
if(inventory[i] != null && id == inventory[i].getId) {
(inventory[size] - inventory[i]);
}
}
} //Close method removeBook
nullin order to lose the reference, but the index doesn't go away. It sounds like what you want is anArrayList, which behaves similarly to an array, except the number of indices is dynamic.