0

So users on my game can have figure codes like ch-255-110.hr-165-1394.hd-180-1380.lg-280-110, I am trying to only fetch certain parts of this figure code, but I am getting an ArrayIndexOutOfBoundsException

Exception message:

java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

Here is the line of code at the highest leve:

hairPart = look.split("hr")[1].split(".")[0];

Whole method:

public static String splitFigure(String look) {
    String hairPart = "";
    String headPart = "";
    String headAccPart = "";
    String hatPart = "";
    String glassesPart = "";
    String beardPart = "";
    String chestPart = "";
    String jacketPart = "";
    String necklacePart = "";
    String pantsPart = "";
    String beltPart = "";
    String shoesPart = "";

    if (look.toLowerCase().contains("hr"))
    {
        hairPart = look.split("hr")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("hd"))
    {
        headPart = look.split("hd")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("he"))
    {
        headAccPart = look.split("he")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("ha"))
    {
        hatPart = look.split("ha")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("ea"))
    {
        glassesPart = look.split("ea")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("fa"))
    {
        beardPart = look.split("fa")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("ch"))
    {
        chestPart = look.split("ch")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("cc"))
    {
        jacketPart = look.split("cc")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("ca"))
    {
        necklacePart = look.split("ca")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("lg"))
    {
        pantsPart = look.split("lg")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("wa"))
    {
        beltPart = look.split("wa")[1].split(".")[0];
    }

    if (look.toLowerCase().contains("sh"))
    {
        shoesPart = look.split("sh")[1].split(".")[0];
    }

    return "ha" + hatPart + "." + "he" + headAccPart + "." + "hr" + hairPart + "." + "hd" + headPart + "." + "ea" + glassesPart + "." + "fa" + beardPart + "." + "ch" + chestPart + "." + "cc" + jacketPart + "." + "ca" + necklacePart + "." + "lg" + pantsPart + "." + "wa" + beltPart + "." + "sh" + shoesPart + ".";
}

Example of what I passed in:

ch-255-110.hr-165-1394.hd-180-1380.lg-280-110
Sam
  • 43
  • 7

2 Answers2

1

Try using this instead:

look.split("hr")[1].split("\\.")[0]

Checked with this

String a = "ch-255-110.hr-165-1394.hd-180-1380.lg-280-110";
System.out.println(a.split("hr")[1].split("\\.")[0]);

Not sure why it's happening but I once faced a similar problem.

mrid
  • 5,782
  • 5
  • 28
  • 71
0
public static void main(String[] args) {

    String look = "ch-255-110.hr-165-1394.hd-180-1380.lg-280-110";
    
    splitFigure(look);
    
    System.out.println(look);
}


public static String splitFigure(String look) {
    String hairPart = "";
    String headPart = "";
    String headAccPart = "";
    String hatPart = "";
    String glassesPart = "";
    String beardPart = "";
    String chestPart = "";
    String jacketPart = "";
    String necklacePart = "";
    String pantsPart = "";
    String beltPart = "";
    String shoesPart = "";

    if (look.toLowerCase().contains("hr"))
    {
        hairPart = look.split("hr")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("hd"))
    {
        headPart = look.split("hd")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("he"))
    {
        headAccPart = look.split("he")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("ha"))
    {
        hatPart = look.split("ha")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("ea"))
    {
        glassesPart = look.split("ea")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("fa"))
    {
        beardPart = look.split("fa")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("ch"))
    {
        chestPart = look.split("ch")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("cc"))
    {
        jacketPart = look.split("cc")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("ca"))
    {
        necklacePart = look.split("ca")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("lg"))
    {
        pantsPart = look.split("lg")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("wa"))
    {
        beltPart = look.split("wa")[1].split("\\.")[0];
    }

    if (look.toLowerCase().contains("sh"))
    {
        shoesPart = look.split("sh")[1].split("\\.")[0];
    }

    return "ha" + hatPart + "." + "he" + headAccPart + "." + "hr" + hairPart + "." + 
    "hd" + headPart + "." + "ea" + glassesPart + "." + "fa" + beardPart + "." + "ch" + 
    chestPart + "." + "cc" + jacketPart + "." + "ca" + necklacePart + "." + "lg" + 
    pantsPart + "." + "wa" + beltPart + "." + "sh" + shoesPart + ".";
}