Solved Seperating a String

Discussion in 'Plugin Development' started by Failplease, Jan 3, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Failplease

    How would I make it so that if a series of characters are in a string, then they separate it into two strings.

    For example, seperating
    Code:
    String s = "I like pie%nIt tastes good.";
    into
    Code:
    "I like pie"
    and
    Code:
    "It tastes good."
     
  2. Failplease
    Use String.split().
    Code:
    String s = "I like pie%nIt tastes good.";
    String[] split = s.split("%n");
     
    String first = split[0]; // I like pie
    String second = split[1]; // It tastes good.
     
  3. Offline

    Failplease

    Assist
    This code:
    Code:
    if(sc.contains("%n%")) {
            String[] s = sc.split("%n%");
            for(int i = 0; s.length >= i; i++) {
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), s[i]);
            }
        }
    gives me an ArrayIndexOutOfBounds Exception, but how could that be?
     
  4. Offline

    Bart

    Code:java
    1. if(sc.contains("%n%")) {
    2. String[] s = sc.split("%n%");
    3. for(String s2 : s)
    4. {
    5. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), s2);
    6. }
    7. }
     
  5. Offline

    Failplease

    Bart
    Trying it out now. Hope it works :D
     
  6. That fails because you have
    Code:
    s.length >= i;
    which should be
    Code:
    i < s.length
    What your code did was loop untill "i" was equal to "s.length" but because arrays count from 0 to the length of the array -1. You would try to access an element after the last element in the array. That doesn't work because there is nothing there.
     
    Assist likes this.
  7. Offline

    Failplease

    blackwolf12333
    Oh lol I didn't know that it looped to the length of the array - 1.

    The code works thanks again.

    Assist and Bart
    I have another problem if you don't mind. I want to string the text on signs together and run it as a command. Here is my code so far:
    Code:
    String str1 = sign.getLine(1) + " " + sign.getLine(2) + " " + sign.getLine(3);
    String str2 = str1.replace("%p%", p.getName());
    Player rndm = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
    String str3 = str2.replace("%r%", rndm.getName());
    cmd.add(str3);
    String sc = StringUtils.join(cmd, ' ');
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), sc);
    (this code is surrounded by this for loop)
    Code:
    for(Location loc = s.getLocation(); loc.getBlock().getType().equals(Material.SIGN_POST) || loc.getBlock().getType().equals(Material.WALL_SIGN); loc.subtract(0, 1, 0)) {
     
    }
    When I right click the sign, it only runs the command on the first sign. What did I do wrong?

    I think I know what I did wrong. Is it that I should run the command outside of the for loop?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
    Bart likes this.
Thread Status:
Not open for further replies.

Share This Page