Is there a way to add integers from an arraylist together?

Viewed 45

I'm trying to add integers from an arraylist together to allow the person to see how many people are queued for a 1v1. This is my code: ` package me.sub.cPractice.Queue;

Main plugin;

public JoinQueue(Main plugin) {
    this.plugin = plugin;
}


@Override
public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    if (cmd.getName().equalsIgnoreCase("queuetest")) {
        Player p = (Player) sender;
        new BukkitRunnable() {
            @Override
            public void run() {
                if (plugin.inqueue.contains(p)) {   
                    String replaced = PlaceholderAPI.setPlaceholders(p, "%server_online%");
                    ScoreHelper helper = ScoreHelper.createScore(p);
                    helper.setTitle("&6&lGoldHQ &r&7┃ &rPractice");
                    helper.setSlot(8, "&7&m---------------------");
                    helper.setSlot(7, "&eOnline: &f" + replaced);
                    helper.setSlot(6, "&eIn Fights: &f" + plugin.infightnumber.toString().replace("[", "").replace("]", ""));
                    helper.setSlot(5, "&7&m---------------------");
                    helper.setSlot(4, "&eQueued For: &fNoDebuff");
                    helper.setSlot(3, "" + plugin.inqueuenumber.toString().replace("[", "").replace("]", ""));
                    helper.setSlot(2, "&7&ogoldhq.net");
                    helper.setSlot(1, "&7&m---------------------");
                }
            }
        }.runTaskTimer(plugin, 20L, 20L);
    }
    return false;
}

` I've been stuck on this for a couple hours now, and I haven't been able to find anything that works. It displays it as 1, 1 when I want it to be displayed as 2. Any help?

1 Answers

First of all your question doesn't contain ArrayList at all. I guess your problem is at line:

helper.setSlot(3, "" + plugin.inqueuenumber.toString().replace("[", "").replace("]", ""));

You can use following code to merge that number's together:

helper.setSlot(3, "" + Arrays.stream(plugin.inqueuenumber.toString()
        .replace("[", "")
        .replace("]", "")
        .split(",")
    ).map(Integer::parseInt)
    .reduce(0, (subtotal, current) -> subtotal + current)
)

Edit: i noticed that plugin.inqueuenumber is that ArrayList you have been talking about so also following should work and previous solution is redundant:

helper.setSlot(3, "" + plugin.inqueuenumber.stream().reduce(0, (l, r) -> l + r))
Related