0

I'm searching a way to continue a for-loop until a string matches.

@SuppressWarnings("deprecation")
public ItemStack[] loadSecoundtLaserInventory(){
    ItemStack pan = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.BLACK.getWoolData());
    ItemMeta panmeta = pan.getItemMeta();
    panmeta.setDisplayName("");
    pan.setItemMeta(panmeta);
    List<ItemStack> content = new ArrayList<ItemStack>();
    content.add(pan);
    //
    for(String str : getConfig().getConfigurationSection("lasers").getKeys(false)){
        if(!str.startsWith("2")){

        }
        String[] amounts = getConfig().getString("lasers." + str + "amount").split(";");
        ItemStack is1 = new ItemStack(getConfig().getInt("lasers." + str + ".block-id"));
        ItemMeta ismeta1 = is1.getItemMeta();
        ismeta1.setLore(Arrays.asList(HiddenStringUtil.encodeString(str)));
        ismeta1.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("lasers." + str + ".dname-amount").replace("{amount}", amounts[0])));
        ItemStack is2 = new ItemStack(getConfig().getInt("lasers." + str + ".block-id"));
        ItemMeta ismeta2 = is2.getItemMeta();
        ismeta2.setLore(Arrays.asList(HiddenStringUtil.encodeString(str)));
        ismeta2.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("lasers." + str + ".dname-amount").replace("{amount}", amounts[1])));
        ItemStack is3 = new ItemStack(getConfig().getInt("lasers." + str + ".block-id"));
        ItemMeta ismeta3 = is3.getItemMeta();
        ismeta3.setLore(Arrays.asList(HiddenStringUtil.encodeString(str)));
        ismeta3.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("lasers." + str + ".dname-amount").replace("{amount}", amounts[2])));
        is2.setItemMeta(ismeta2);
        if(!content.contains(is1)){
            content.add(is1);
        }
        if(content.size() == 2){
            content.add(pan);
            content.add(pan);
        }
        if(!content.contains(is2)){
            content.add(is2);
        }
        if(content.size() == 5){
            content.add(pan);
            content.add(pan);
        }
        if(!content.contains(is3)){
            content.add(is3);
        }
        if(content.size() == 8){
            content.add(pan);
            return content.stream().toArray(ItemStack[]::new);
        }
    }
    return null;
}

Basically the Strings i loop over is something like

1panda,2boom,3tnt.

The problem is when the string doesn't starts with 2, i want to continue to the next String. I cannot use the return statement, because it would stop the loop. Any other ideas?

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79

1 Answers1

0

You can use continue;. What is the "continue" keyword and how does it work in Java?

for (int i = 0; i < 100; i++ ) {
    if(i % 2 == 0) {
        continue;
    } else {
        System.out.println(i);
    }
}

Will print only the odd numbers. Obviously, that's a bit of a contrived example, since you could have just done the print on if(i % 2 !=), but I hope you get the idea.

Community
  • 1
  • 1
Fodder
  • 564
  • 1
  • 10
  • 23