Coming soon - Get a detailed view of why an account is flagged as spam!
view details
2
[Java] I have an if statement / else if statement not executing.
Author Summary
pds12345 is in Java
Post Body

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()

Author
Account Strength
100%
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
8,864
Link Karma
399
Comment Karma
8,066
Profile updated: 2 days ago
Posts updated: 8 months ago

Subreddit

Post Details

Location
We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
10 years ago