So I'm a scrub at java, just started learning the language at a university course.
While trying to compare values of variables linked to objects to inserted values to make sure that there are no duplicates, I ran into an issue. It appears as if the objects are "lagging behind" in the lack of a better expression. See the commenting of the code for a better explanation.
The problem only appears if you add like 2 or more separate weapons and then try to re-add a weapon with the same name, it allows you to do so. If you add just one one weapon and then try to re-add it it says nope and the code works as intended.
So yeah, when I call the method printHeroInfo I still see that I can end up with several weapons with the same name. There is also another class called superWeapon, and if you think the code in there is relevant to this issue then let me know and I'll post that in a commment or something.
Thanks in advance for helping me with the head scratching.
import java.util.ArrayList;
public class superHero{
private ArrayList<superWeapon> superWeaponList;
private String superHeroName;
private String superHeroInfo;
public superHero(String superHeroName, String superHeroInfo){
this.superHeroName=superHeroName;
this.superHeroInfo=superHeroInfo;
superWeaponList=new ArrayList<superWeapon>();
}
public void addSuperWeapon(String superWeaponName, int superWeaponCharges){
superWeapon Obj = new superWeapon(superWeaponName, superWeaponCharges); // Making an object of the superWeapon class.
int size=superWeaponList.size(); // Gets the size of the array superWeaponList.
if(size==0){ /* If the size of the arary is zero, that means there are no weapons in it currently.
For that reason we don't need the for-loop we otherwise would need to compare the name of the weapon
we are trying to add against the pre-existing names of weapons in the array. */
superWeaponList.add(Obj);
System.out.println("The Superweapon"+superWeaponName+" has been added for the superhero "+superHeroName+".");
System.out.println(superWeaponName+" has "+superWeaponCharges+" charges.");
}
else{ /* If the size of the array is NOT zero, we need to compare the name of the weapon we are trying to add
against the weapons already existing in the array. This we do with the following for loop. */
for(int i=0;i<size;i++){
superWeapon temp_obj=superWeaponList.get(i);
String temp_name = temp_obj.getName();
System.out.println(temp_name); /* Why does the name lag behind one entry?
On the second entry it also shows the first entry. On the third entry it shows the second
entry and so on... The first place in an arraylist should be index=0,
so the for-loop starting at 0 should also be correct, right?
I just added this print to see what the current value for getName()
would end up returning. This is how I found the bug/error. */
if(temp_name!=superWeaponName){ // If the name doens't match then we add a new superweapon.
superWeaponList.add(Obj);
System.out.println("The superweapon "+superWeaponName+" has been added for the superhero "+superHeroName+".");
System.out.println(superWeaponName+" has "+superWeaponCharges+" charges.");
}else{ // If the names match however, we don't add the new weapon we were trying to add.
System.out.println("There already is a superweapon with the name "+superWeaponName+
" registered for the superhero "+superHeroName);
}
}
}
}
public void printHeroInfo(){
System.out.println(superHeroName);
System.out.println(superHeroInfo);
int size=superWeaponList.size();
for(int i=0;i<size;i++){
superWeapon temp_obj=superWeaponList.get(i);
System.out.println(superHeroName+" has the superweapon "+temp_obj.getName()+" with "+temp_obj.getCharges()+" charges.");
}
}
}