I am having an issue where when I give input, the input either being a 1 or a 2, my code will skip past my if and else if for 1 or 2. I'm not sure what all code I should post, and I am not going to paste my whole project. But if needed, I can post more of what is needed.
if (userChoice == 1 && me.getTreasure().usePotion() == true){
This is the statement that is being skipped over, obviously I know my userChoice is == 1, so with that I am unsure if I am implementing my method wrong or what. This method is supposed loop through my ArrayList, items, and each loop it will search for the index of "Potion" and if the index is >= 0 I use items.remove to remove that first occurrence from the list and return true. Other wise, if the whole list gets iterated through, return false.
Here is where I make my ArrayList in main and pass it to Treasure.
//Treasure ArrayList
ArrayList<String> items = new ArrayList<>();
items.add("Potion");
items.add("Potion");
items.add("Scroll");
Treasure meTre = new Treasure(items);
import java.util.ArrayList;
public class Treasure {
private ArrayList<String> items = new ArrayList<>();
public Treasure(ArrayList<String> itemsx){
items = itemsx;
}
public ArrayList<String> getTreasure(){
return items;
}
public boolean usePotion(){
for (int findPot = 0; findPot < items.size(); findPot ){
if (items.indexOf("Potion") >= 0){
items.remove("Potion");
return true;
}
}
return false;
}
Edit: in another class I also have a method named getTreasure that returns my treasure object, this is why I have me.getTreasure().usePotion()
Subreddit
Post Details
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...