Looping through lines

Discussion in 'Spigot Plugin Development' started by Zain20, May 30, 2017.

  1. Hello, I was making my TempBan class into a method so I can use it easier in my ServerCore, but, I have run into an issue. How could I make it so I can loop through the lines of a String and make them all into another String? I want to be able to do like "Line1;;Line2;;Line3". I know how to make this an ArrayList but how could I make this a String so I can put it in the.kickPlayer method. I do not know how I can make an ArrayList and put it in the .kickPlayer method, may I have some help?

    My method:
    Code (Text):
        public static void tempBan(Player player, int time, String unit, String kickText) {
            endOfBan = System.currentTimeMillis() + BanUnit.getTicks(unit, time);
            setBanned(player.getName().toLowerCase(), endOfBan);

            String message = getMSG(endOfBan);
         
            String[] kicklines = kickText.split(";;");
            if (!(kicklines == null))
                for (int i = 0; i < kicklines.length; i++)
                    player.kickPlayer(kicklines[i].toString().replaceAll("&", "ยง") + ";;".replaceAll(";;", "").replace("%time%", message));
         
        }
    Right now, it only says the first line I put when they are kicked.

    - Zain20
     
    #1 Zain20, May 30, 2017
    Last edited: May 30, 2017
  2. Choco

    Moderator

    Code (Java):
    String[] variables = yourString.split(";;");
     
  3. Oops! I was very unclear, let me edit the thread!
     
  4. You could just store the list of strings somewhere and rather use String#join.
     
    • Like Like x 1
    • Agree Agree x 1
  5. Thank you for the answers! It is a huge help. :)
     
  6. You are also kicking the player multiple times in the loop. You could just create a new StringBuilder and append(kicklines[j]).append("\n") then just call that string builder toString() in the kick message after the loop is done. \n is the line separator or just replace(";;", "\n) on the whole string.

    Also why a for int I ; I++ loop? You could just use a for (String line : kicklines) {
    // append here
    }
    // kick here with StringBuilder#toString()