#Why is my program not detecting if list contains specific object?

1 messages · Page 1 of 1 (latest)

late stag
#

I am working on an online course and this is one of the problems.

It wants me to create a program so that books that are already on the list are not added again. Two books should be considered the same if they have the same name.

I need to create an equals method so I can use the contains method to properly search my list for repeating objects.

The program does not seem to be reading my second if statement in main. When I enter in a second book with the same title as the first, it asks for the publication year. I want it to print my message asking for a different book and continue back at the beginning of the while loop.

shell laurel
#

your link doesn't work

late stag
#
#

Hey, sorry about that. Hope this works

shell laurel
#

contains(name) is wrong

#

books.contains(String) is always false

#

wrong type

#

do a linear search worst case

#
#

this one is fixed ^

#
#

works fine 🙂

#

I didn't use your Book.equals() function

late stag
#

Thanks for responding! I moved the if statement after the creation of the book object and did contains(book) instead of contains(name) and it works now.

#

Yeah the course im taking wants me to use that function specifically i dont know if it would accept the answer otherwise

shell laurel
#

contains(book) does NOT work

late stag
#

its pretty specific

#
#

this is what i did

shell laurel
#

whatever man

#

I gave you the best solution

#

do whatever you want

#

🤷‍♂️

late stag
#

I dont doubt you did

#

thanks for helping, I appreciate your time

late stag
# shell laurel do whatever you want

Im sorry, Im not trying to ignore your help. Im just trying to follow the course and right now it wants me to modify an equals function to use contains to compare objects on a list. Not sure if Im saying that all right but yeah. Im sure linear search is a better solution, just not the one they were trying to teach me.

#

I really appreciate your help and your time really thank you

shell laurel
#

contains() does a linear search for array list

late stag
#

interesting, so its just a different way of writing it out?

shell laurel
#
 public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }

    /**
     * Returns the index of the first occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the lowest index <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     */
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
#

if (o.equals(elementData[i]))

late stag
#

or contains is a premade function to do a linear search of an arraylist?

shell laurel
#

yes and the implementation for jdk7+ is above

late stag
#

gotcha that makes sense

shell laurel
#

it will do book.equals(elementData[i])

#

the reason it's bad practice is that equals() implementation would vary

#

so it's better to do a search using the specific fields for filtering

#

for Map<> you can do containsKey() or get() != null

late stag
#

So are you saying that by modifying an equals function to compare objects, Im locking myself into using that equals function for only one thing? In this case, comparing book objects of name and year.

shell laurel
#

that also explains why your equals() didn't have publicationYear

#

in this case only by name

#

year is missing on purpose

#

which would be bad practice

late stag
#

yeah thats my bad

#

I was supposed to do it by name and year, sorry for any confusion

#

After you sent your initial message, i realized i was supposed to compare the objects as a whole, name and year. That was why I messed up, I was trying to only compare it to name. Which is why I put the name string in my contains function which was a mistake

#

I modified the equals() to have name and year afterwards

shell laurel
#

if you add the year

#

then the contains() logic won't work

#

that's why I did the getBook() check

#

but if your teacher says use equals() / contains() then you had it correct

late stag
#

Yeah based on the info I gave you, it makes sense why you did it like that

#

its just an online course but yeah

shell laurel
#

You could have Deitel 2020 and Deitel 2010 and Deitel 2022

#

different editions of the same Java textbook

late stag
#

Thanks for the recc! Do you think thats the best way to learn java as a beginner, or something to transition to after finishing this course for more advanced studies?

#

I am learning java on my own while working full time. Not sure the best way to go about it. Im almost halfway through this course and feel good about it so far. Its ramped up quite a bit but there are so many problems at each level of complexity I never feel too lost. I appreciate any advice.

#

Trying to switch careers desperately and maybe get a job in web dev, figure it would be the easiest to start with considering I dont have a CS degree. I looked up job postings and saw java as a common requirement so I chose that. Planning to learn some frameworks and do some small projects after Im done with this course

shell laurel
#

read the Deitel book in resource

#

I dislike the way they handle Exception early on, by IGNORING best practice

#

but it's a good book

#

plus your teacher is also doing it WRONG x2

late stag
#

lmao x2

shell laurel
#

his demo code is resleak x2

#

but so is deitel

#

everyone who comes in this channel does resleak

#

I am like 🤦‍♂️

#

let's make sure beginner starts with horrible habbit from the get go

#

because closing your trash resource is too complicated 🤦‍♂️

late stag
#

ahh memory management

shell laurel
#

not memory

#

resource

late stag
#

gotcha

shell laurel
#

anything AutoCloseable or Closeable

#

Deitel starts addressing that in Chapter 13 or 15+

#

you should minimally call scanner.close();

late stag
#

Well I definitely dont want to be building bad habits, thats a pitfall Im trying to avoid by being self taught

#

Although at some level that may be impossible

shell laurel
#

well, use Eclipse with SpotBugs plugin

#

or the older version FindBugs

#

Basically, you want to write your code a certain way

#

to avoid all potential bugs

#

and to have static checkers tells you in your FACE

#

how bad your code is

#

before you start hitting RUN

#

that's the idea 💡

late stag
#

Thanks Ill check that out

#

Ill check out the book too