Skipping a current iteration for loop

Discussion in 'Plugin Development' started by greeves12, Oct 1, 2017.

Thread Status:
Not open for further replies.
  1. Offline


    I've created a bug reporting system that keeps user created bugs in a UI. My problem is when someone deletes a report for example 8 and there are 20 reports total, the for loops will generate and fill the inventory will reports until 7. How could I get the for loop to skip 8 and move to 9? I've tried using continue; and it doesn't seem to work.

    public static void createInv() {
            for (int k = 0; DataFile.getData().contains("Reports." + k); k++) {
                    if (k < 53) {
                        if(!DataFile.getData().contains("Reports." + k)){
                        if(DataFile.getData().contains("Reports." + k)) {
                            String player = DataFile.getData().getString("Reports." + k + ".Player");
                            String title = DataFile.getData().getString("Reports." + k + ".Title");
                            ItemStack stack = new ItemStack(Material.BOOK, 1);
                            ItemMeta meta = stack.getItemMeta();
                            ArrayList<String> lore = new ArrayList<String>();
                            meta.setDisplayName("§aReport Number: §d" + k);
                            lore.add(("By: " + player));
                            lore.add(("Title: " + title));
                            Main.reportInv.setItem(k, stack);
                    if (k == 53) {
            ItemStack anvil = new ItemStack(Material.ANVIL, 1);
            ItemMeta Meta = anvil.getItemMeta();
            Meta.setDisplayName("§bNext Page");
            Main.reportInv.setItem(53, anvil);
  2. Offline


    Use an if statement in the for loop to check if the report is not the one you want to remove.

    for (int i = 0; i < 20; i++) {
            if (i != 7) {
  3. Offline


    First, don't use indexes for the keys. All that does is leave the potential for blank spaces. Instead, use getData().getConfigurationSection("reports").getKeys(false) to get all the keys in the reports. With that, you can replace k with anything (such as the user's name/uuid, or a random value if you can't have duplicate values.) This is one way to fix your problem, and is the one you should do.

    If you really need indexes for some reason, then change the middle parameter in the for loop to true, since what that currently does is check if the config contains an index, and once it find one that is not in the config, it breaks. Replacing that with true will mean that it will only break when you tell it to.

    BTW: You don't need the second contains if statement. Since you continue if it does not exist, the only way to get to the rest of the code is if it does contain the path.
  4. Offline


    So change the for loop to for(int k = 0; DataFile.getData.getConfigurationSection("Reports.").getKeys(false); k++)?
  5. Offline


    No. That should be underlined as that returns a Set<String>, not a boolean. Instead, you want to create an enhanced for loop. Something like for(String k : .....) is what you want.
  6. Offline


    Ok I see what you mean. Thanks
  7. Offline


    Use if statement and "continue" (used to skip a loop runtime)

    Sent from my LG-H860 using Tapatalk
Thread Status:
Not open for further replies.

Share This Page