Solved String split not working

Discussion in 'Plugin Development' started by CraftCreeper6, Aug 28, 2019.

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

    CraftCreeper6

    Hello, got a pretty strange problem.

    I take in a string which is a configuration path like the following one:

    WIN_TYPES.Epic.chance

    Code:
    for (String s : this.getConfig().getKeys(true))
            {
                if (s.contains("."))
                {
                    String[] spl = s.split(".");
                    //System.out.println(split[0]);
                    System.out.println(spl.length + " : " + s.toString());
                    if (spl.length == 1)
                    {
                        if (spl[0].equalsIgnoreCase("WIN_TYPES"))
                        {
                            String name = spl[1];
                            float chance = Float.parseFloat(this.getConfig().getString(s + ".chance"));
                            String symbol = this.getConfig().getString(s + ".symbol");
                        
                            Option o = new Option(name, chance, symbol);
                            options.addOption(o);
                        }
                    }
                }
            }
    Code above.

    The string is not split, the commented out line throws an error; java.lang.ArrayIndexOutOfBoundsException to be exact. So I removed the line of code and no longer have the error (so that's definitely the problem)

    The results of the debug are as follows:
    Code:
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Epic
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Epic.chance
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Epic.symbol
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Standard
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Standard.chance
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Standard.symbol
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Noob
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Noob.chance
    [22:01:22] [Server thread/INFO]: 0 : WIN_TYPES.Noob.symbol
    So as you can see there's no reason why it should not be split at "."

    Any help is appreciated.

    EDIT: Just found the answer, string split will not work on . as it is used in regular expressions to denote "all". Simply adding \\ to the start should fix this issue.
     
    Last edited: Aug 28, 2019
  2. Offline

    Strahan

    Bear in mind, you should be validating data too. The parsing could fail and the getConfig() could return null, both of which would cause the plugin to crash. Also you are, while inside a logic block where you check if array length is 1, make a call to array[1] which will cause a crash.
     
  3. Offline

    CraftCreeper6

    @Strahan
    Checks are completed before that code runs, everything should be okay.
     
  4. Offline

    Strahan

    Well, I can only comment on what I see. You didn't post the whole class. Checks though should be done close to the point of use. If you are checking prior, it must mean you are pointlessly iterating the config again.
     
  5. Offline

    CraftCreeper6

    @Strahan
    I use the config before the lines of code I sent. So checks are necessary before what is seen.
     
Thread Status:
Not open for further replies.

Share This Page