1

I'm trying to figure out how to delete a specific line in my Arraylist when a user types in a book ID number. However it seems that it can't find that ID number anywhere in my Arraylist.

private void removeBook() 
    // Removes a certain book from the Book List.
    {
        Scanner IDS = setbookID();
        int idNum = IDS.nextInt();
        if(bookList.contains(idNum)){ //problem
            bookList.remove("B"+Integer.valueOf(idNum));
        }
    }

private Scanner setbookID(){
    Scanner bookID = new Scanner(System.in);
    System.out.println("Enter your book's ID number. ");
    return bookID;}

bookList is an ArrayList that read through a text file and come out as a String. A line in my text file looks like this:

B998 ; Aithiopika ; Heliodorus ; 1829

However if the user types in "998" it doesn't remove this line from the ArrayList. Any ideas on how I can go about doing this? Would an iterator somewhat help?

EDIT: This is how I added the books to the ArrayList in the first place.

private ArrayList<Book> readBooks(String filename) {
        ArrayList<Book> lines = new ArrayList<>();
        readTextFileB(filename, lines);
        return lines; 
    }

private void readTextFileB(String filename, ArrayList<Book> lines) 
    // Reads the books.txt file.
    {
        Scanner s = null;
        File infile = new File(filename);
        try{
            FileInputStream fis = new FileInputStream(infile);
            s = new Scanner(fis);
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        while(s.hasNextLine())
            lines.add(new Book(s.nextLine()));
    }
Michael
  • 45
  • 5
  • There are a lot of potential causes here. Can you show your ArrayList and how you fill it with books? Do you remove the 'B' from the ID before adding it? Do you add it as an `Integer`? – Zircon Sep 13 '17 at 13:56
  • Is it possible that you have several occurences of this ID ? as javadoc says "public boolean remove(Object o) Removes **the first occurrence** of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged.". So you should start by checking there is only one occurence, and also if you can find it in the list before you try to delete it. – derOtterDieb Sep 13 '17 at 13:59
  • Possible duplicate of [Check if ArrayList contains part of a string](https://stackoverflow.com/questions/6428005/check-if-arrayliststring-contains-part-of-a-string) or https://stackoverflow.com/questions/8192665/how-to-search-for-a-string-in-an-arraylist or https://stackoverflow.com/questions/6645379/partially-match-strings-in-case-of-list-containsstring – Tom Sep 13 '17 at 13:59
  • @Zircon I edited the question and added how I read the lines. I add the ID as an Integer and then I add the "B" right before that Integer when I turn the entire line into a string. – Michael Sep 13 '17 at 14:06
  • Since you are using to distinct value for `contains` and `remove`, this is a first problem. Also, if you called those too method, you should use `indexOf` instead of `contains` and use the index to `remove` a specific value. Last, you need to show use `Book.equal` to be able to see how `contains` or `indexOf` should be able to match those (but since you use a `String`, that's not a good start either. – AxelH Sep 13 '17 at 14:09

1 Answers1

0

You have multiple problems with your code. Not clear what you are trying to achieve whether you want to remove the Book object from the list. If so then you need to compare the ID field(using iterator) of the book object assuming your Book class is like below:

    class Book {
    int id;

    Book(int id) {
        this.id = id;
    }
}

If above case is not the scenario then your list is a list of Book object then how can you pass String as parameter on the remove method bookList.remove("B"+Integer.valueOf(idNum)); you should pass Book object here or index number.

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23