Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code works well but the problem is the same, even if the bird species is already in the list, I get the message that a new bird has been added to the list. When I change (found) to (! Found) instead, I get the second message that the bird is already in the list even though it is not in the list.

What I have tried:

public void addBird(String art, String location){
boolean found = false;
for (int i = 0; i <birdlList.size(); i++){

if(birdList.contains(art))
found = true;
break;
}

if (!found) {
System.out.println("The bird is already in the list");

} else {
birdlList.add(new Bird(art, location));
System.out.println("A new bird has been added to the list");
}
}
Posted
Updated 14-Aug-18 13:49pm

By looking at the code, i think the problem is on this block. If you format the code nicely, you will see the loop only run once and exit immediately
Java
for (int i = 0; i < birdlList.size(); i++) {

  if (birdList.contains(art))
   found = true;
  break;
 }


I think this is what you want in the code. if the condition is true, set found = true and then exit the loop.
Java
for (int i = 0; i <birdlList.size(); i++)
{
    if(birdList.contains(art)) {
        found = true;
        break;
    }
}
 
Share this answer
 
1) Java and JavaScript are 2 different beast, you need to make your mind and choose 1 of them.
2) Your code is weird:
Java
for (int i = 0; i <birdlList.size(); i++){
    if(birdList.contains(art))
        found = true;
    break;
}

you make a loop that will run only once, and you don't use the counter? you can simplify to:
Java
for (int i = 0; i <birdlList.size(); i++){
    if(birdList.contains(art))
        found = true;
    break;
}

3)
Quote:
The code works well but the problem is the same,

Translation: the code don't work.
Use the debugger to watch your code perform and locate where it go wrong.
One can suspect birdList.contains(art) to be wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900