1

My loop is only checking the first item of the array, which makes my code fail each time:

String igitems = "IgnoredItems";
String items = getConfig().getString("IgnoredItems.itemid");
items = items + args[0] + ",";
String[] myarray = items.split(",");
for(String fitem : myarray) {
    if(!fitem.equals(args[0])){
        getConfig().set(igitems + ".itemid", items);
        saveConfig();
        reloadConfig();
        sender.sendMessage(prefix + ChatColor.GREEN + "Added " + ChatColor.GOLD + args[0] + ChatColor.GREEN + " to ignore list.");
        break;
   } else {
        sender.sendMessage(prefix + ChatColor.RED + "Item is already in the list");
        break;
    }
}

If I add system.out.print(myarray); after String[] myarray = items.split(","); it shows the entire array, which is right:

1:0
2:0
5:0
8:0
5:0

But, when I add system.out.print(fitem); after for(String fitem : myarray){ it shows only the first item from the array (1:0) wich makes my code fail almost each time except if it's the first item.

Elazar
  • 20,415
  • 4
  • 46
  • 67
ax752
  • 142
  • 2
  • 12
  • 2
    Does it make sense to `break` in both the `if` and the `else` part? – M A Oct 27 '14 at 21:48
  • It should only find the item once, if it finds it, that's why I break it. – ax752 Oct 27 '14 at 22:01
  • you are breaking on if AND else. So you are checking the first element and it results in a break, even if it isnt equal. delete the break from the `else` – Terry Storm Oct 27 '14 at 22:07

4 Answers4

0

You are breaking the loop. I dont know in which case you want to break the loop but even if fitem.equals(args[0]) or !fitem.equals(args[0]) you are invoking the break.

Maybe you just want to break the else case?

if(!Arrays.asList(my).contains(args[0])){
    getConfig().set(igitems + ".itemid", items);
    saveConfig();
    reloadConfig();
    sender.sendMessage(prefix + ChatColor.GREEN + "Added " + ChatColor.GOLD + args[0] + ChatColor.GREEN + " to ignore list.");
}else{
    sender.sendMessage(prefix + ChatColor.RED + "Item is already in the list")
}

A break will return from the nearest loop. So in your case you stop iterating through for(String fitem : myarray) A continue instead will immediatly go one step ahead in your loop

user
  • 735
  • 1
  • 6
  • 21
0

Take a look at this question and its answers - you're break statements are causing this behaviour. You probably want continue or just nothing at all, since that would have the same behaviour.

Community
  • 1
  • 1
Jeff
  • 12,555
  • 5
  • 33
  • 60
0

You are adding , after and not between.

String items = getConfig().getString("IgnoredItems.itemid");
// Wrong
items = items + args[0] + ",";
// Ok
if (items.isEmpty()) {
    items = args[0];
} else {
    items = items + "," + args[0];
}

However, review your entire approach. See MemorySection.getStringList(String).

List<String> items = getConfig().getStringList("IgnoredItems.itemid");
items.add(args[0]);
getConfig().set("IgnoredItems.itemid", items);
spongebob
  • 8,370
  • 15
  • 50
  • 83
0

I replaced the loop with

                    if(!items.contains(args[0] + ",")){
                        getConfig().set(igitems + ".itemid", items + args[0] + ",");
                        saveConfig();
                        sender.sendMessage(prefix + ChatColor.GREEN + "Added " + ChatColor.GOLD + args[0] + ChatColor.GREEN + " to ignore list.");
                    }  else {
                        sender.sendMessage(prefix + ChatColor.RED + "Item (" + ChatColor.GOLD + args[0] + ChatColor.RED + ") is already in the list !");
                    }

and it worked fine, solved.

ax752
  • 142
  • 2
  • 12