Looping through array string

Discussion in 'Plugin Development' started by Infernus, Feb 27, 2011.

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

    Infernus

    Okay forget about all the above again.

    PHP:
        if(split[0].equalsIgnoreCase("/a") && player.isOp()) {
            for(
    Player P plugin.getServer().getOnlinePlayers()) {
                if(
    P.isOp())
                    
    P.sendMessage("<" player.getDisplayName() + "> " ChatColor.YELLOW "Adminchat: " split[1]);
            }
            
    event.setCancelled(true);
        }
    In this source you can see I am working on an adminchat plugin.

    Now the only thing I want is get everything AFTER split[0].

    Things I have already tried:

    PHP:
        if(split[0].equalsIgnoreCase("/a") && player.isOp()) {
            
    int i 1;
            
    String message "";
            while(
    <= split.length) {
                
    message += " " split[i];
                
    i++;
            }
            for(
    Player P plugin.getServer().getOnlinePlayers()) {
                if(
    P.isOp())
                    
    P.sendMessage("<" player.getDisplayName() + "> " ChatColor.YELLOW "Adminchat:" message);
            }
            
    event.setCancelled(true);
        }
    Also tried:

    PHP:
        if(split[0].equalsIgnoreCase("/a") && player.isOp()) {
            
    int i 1;
            
    String message "";
            while(
    split.length) {
                
    message += " " split[i];
                
    i++;
            }
            for(
    Player P plugin.getServer().getOnlinePlayers()) {
                if(
    P.isOp())
                    
    P.sendMessage("<" player.getDisplayName() + "> " ChatColor.YELLOW "Adminchat:" message);
            }
            
    event.setCancelled(true);
        }
    Both gave me an out of range error on the array.. Would really appreciate some help :)
     
  2. Offline

    DiaAWAY

    whell, in the first example, you're doing i <= split.length, so you're using i to look in the split array where i eventually becomes the length of the array itself.

    array = {1, 2, 3} => array.length = 3
    array [0] = 1
    array [1] = 2
    array [2] = 3
    array [3] => error out of range.

    see the problem now? ;)

    EDIT: bah, nvm i see that you already tried that in the second code xD
    EDIT: this is on the onCommand, right?
     
  3. Offline

    Infernus

    Yeah it is, and thanks for your effort :)
     
  4. Offline

    DiaAWAY

    at what line are you getting this error? I am assuming its the message += line...
    try printing out the length of the array you're working with and printing out the value of i?
     
  5. Offline

    Infernus

    Woah, the second one seemed to work after all. I don't know why it didn't before, but it completely works now! Thanks for your time and effort! :)
     
  6. Offline

    DiaAWAY

    Haha! Good to know! I was racking my brain on that one, because i couldn't see ANY reason for it not working! xD
     
  7. Offline

    Raphfrk

    Java really needs an implode method.

    split.implode(1, 7, " ")

    would give a string that split[1] to split[7] with each one separated by a space.
     
  8. Offline

    Infernus

    Something like this, I have lack of syntax knowdledge in Java to make this a function string..

    PHP:
    void String Implode(String[] inint startint end) {
            
    String s "";
            while(
    start end) {
                if(
    s.length() != 0)
                    
    += " " in[start];
                else
                    
    += in[start];
                
    start++;
            }
            return 
    s;
        }
     
Thread Status:
Not open for further replies.

Share This Page