0

I have a String named listOfItemsBanned, and an ArrayList named itemsBanned. Let's say the ArrayList itemsBanned contains 3 things: TNT, EnderPearl, and Sand. I would want the String to be "TNT, EnderPearl, Sand". And when something is removed from itemsBanned, it would remove it from the string, too.

So.. I want to be able to get every item included in an ArrayList and put it into one String with each item separated by commas.

1
  • 1
    Iterate the List and append the content to a StringBuilder and when you're done, use the StringBuilders toString method to get the String. You will have to do this each time you want to update the String, there's no way to automate (so that adding a value to the List magically creates the String) - unless you really want to write some kind of wrapper class Commented Mar 12, 2014 at 1:02

3 Answers 3

2

You need only one line:

String listOfItemsBanned = itemsBanned.toString().replaceAll("^.|.$", "");

toString() of List produces a CSV of the elements, but wrapped in [ and ]. The call to replaceAll() removes the first and last characters to leave just the elements.

Sign up to request clarification or add additional context in comments.

1 Comment

very elegant my dear new moderator ;)
1

You could do this:

String listOfItemsBanned = "";

for(int i = 0; i < itemsBanned.size(); i++){ //loop threw contents of itemsBanned (List<String>)
    String s = itemsBanned.get(i); //get the string in itemsBanned (i)
    listOfItemsBanned = listOfItemsBanned + "," + s; //add s to listOfItemsBanned
}

Now, if you would like to get all of the items that are banned from the string listOfItemsBanned, you could do:

String[] s = listOfItemsBanned.split(",");

//start the for loop at 1 because when saved it will be ",TnT,Sand,Enderpearl", notice the extra , in the begining
for(int i = 1; i < s.size(); i++){ //loop threw all of the items banned.
    String itmBanned = s[i];
}

You could now do anything with itmBanned, like convert it to a Material:

Material m = Material.getMaterial(itmBanned);

So, you could do something like this for a remove method:

public void remove(String type){
    String listOfItemsBanned = "";

    itemsBanned.remove(type); //remove 'type' from the array

    for(int i = 0; i < itemsBanned.size(); i++){
        String s = itemsBanned.get(i);
        listOfItemsBanned = listOfItemsBanned + "," + s; //reset the string to the new list
    }
}

and for adding:

public void remove(String type){
    String listOfItemsBanned = "";

    itemsBanned.add(type); //add 'type' to the array

    for(int i = 0; i < itemsBanned.size(); i++){
        String s = itemsBanned.get(i);
        listOfItemsBanned = listOfItemsBanned + "," + s; //reset the string to the new list
    }
}

then you could check if the player is using a banned item, and cancel the event if they do, an example if they're using a banned block, like sand or TnT would be:

@EventHandler
public void playerInteract(PlayerInteractEvent e){
    if(e.getAction.equals(Action.RIGHT_CLICK_AIR) || e.getAction.equals(Action.RIGHT_CLICK_BLOCK){
        //the user has right-clicked
        Material m = e.getItemInHand().getType(); //get the item in the user's hand
        if(m != null){ //check that it's not null
            if(listOfItemsBanned.contains(m.getName()){ //if listOfItemsBanned has the name of the item in the player's hand in it
                e.setCanceled(true); //cancel the block place
            }
        }
    }
}

1 Comment

just FYI, use a StringBuilder when concatenating string inside a loop
0

Imports:

import java.util.Arrays;
import java.util.List;

Code:

public static void main(String args[]) {
    String[] listOfItemsBanned = { "TNT", "EnderPearl", "Sand" }; // ArrayList
                                                                    // of
                                                                    // banned
                                                                    // items
    String output = ""; // Creates output String
    for (int i = 0; i < listOfItemsBanned.length; i++) { // Loops through
                                                            // all items in
                                                            // the ArrayList
        output += listOfItemsBanned[i]; // Adds item to String
        if (i != listOfItemsBanned.length - 1) { // If it is not the last
                                                    // item in the ArrayList
                                                    // add ", "
            output += ", ";
        }
    }
    System.out.println(output); // Output String
}

Output:

TNT, EnderPearl, Sand

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.