Solved Delete line from config (ArrayList)

Discussion in 'Spigot Plugin Development' started by Attacler, Apr 19, 2017.

  1. Hello there,

    I want to delete a line out of an arraylist if it contains something, i already tried some things but it didnt work, can you help me?

    Bye,
    Bart
     
  2. Create a numeric for loop that iterated through your arraylist, get the string using the index, check it and delete the string at the index when it should be.
     
  3. This is quite simple to do, First get the List from the config onto a List object, then remove the unneeded entry. Then set the new List to the path of the arraylist in the config.
    Hope this solves your question.
     
  4. Well what have you tried so far?
    This should work.
    Code (Text):
    List<String> yourList = new ArrayList<String>();

            if(yourList.size() != 0)
            {
                for (String s : yourList)
                {
                    if (s.equalsIgnoreCase("your String"))
                    {
                        yourList.remove(s);
                    }
                }
            }
     
  5. This doesnt work because it is a name with coordiantes :D
    Thanks, ill try it now!
     
  6. It doesnt work, what i tried (invc is a config):
    Code (Text):
                                List<String> list = invc.getStringList(defaultpath);
                                String c = ",";
                                if(list.contains(isclaiming.get(p))){
                                for(int x = 0; x < list.size(); x++){
                                    String s = list.get(x);
                                    if(s.contains(isclaiming.get(p))){
                                        list.remove(x);
                                    }
                                }
                                }
     
  7. @Attacler What is 'P' and what is 'isclaiming'?
     
  8. Strahan

    Benefactor

    You cannot loop like that and modify the thing you are looping. Use an iterator.
     
  9. P is a player and isclaiming is a hashmap <Player,String>
    How?
     
  10. Try this (and don't forget setting the updated list into the config at the end as well as saving it)
    Code (Text):

            List<String> list = invc.getStringList(defaultpath);
            HashMap<String, Player> isclaiming = new HashMap<String, Player>();

            if(list.contains(isclaiming.get(player)))
            {
                for(String s : list)
                {
                    if(s.contains(isclaiming.get(player)))
                    {
                        list.remove(s);
                    }
                    invc.set(defaultpath, list);
                    invc.saveConfig();
                }
            }
     
  11. This is already causing it to not work if you want to check if any of the values contains it.
     
  12. ..... what about a simpler way like this....
    Code (Text):
    List<String> list = invc.getStringList(defaultpath);
                                String c = ",";
                                if(list.contains(isclaiming.get(p).getName())){
                                list.remove(isclaiming.get(p).getName());
                                }
    invc.set(defaultpath,list);
    invc.save();
     
  13. It isnt about player names but about strings, isclaming is a hasmap <Player, String>

    Thanks it worked now!