英文:
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 + ".";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论