Java: 长度为 0,索引 0 超出范围

huangapple go评论69阅读模式
英文:

Java: Index 0 out of bounds for length 0

问题

以下是您要翻译的代码部分的内容:

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 + ".";
}

如果您需要更多的翻译或有其他问题,请随时告诉我。

英文:

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

答案1

得分: 1

尝试使用这个替代:

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

使用以下代码进行检查:

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

不确定为什么会发生这种情况,但我曾经遇到过类似的问题。

英文:

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.

答案2

得分: 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 + ".";
}
英文:
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 + ".";
}

huangapple
  • 本文由 发表于 2020年10月24日 14:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64510688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定