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

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

Java: Index 0 out of bounds for length 0

问题

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

  1. Exception message:
  2. java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
  3. Here is the line of code at the highest leve:
  4. hairPart = look.split("hr")[1].split(".")[0];
  5. Whole method:
  6. public static String splitFigure(String look) {
  7. String hairPart = "";
  8. String headPart = "";
  9. String headAccPart = "";
  10. String hatPart = "";
  11. String glassesPart = "";
  12. String beardPart = "";
  13. String chestPart = "";
  14. String jacketPart = "";
  15. String necklacePart = "";
  16. String pantsPart = "";
  17. String beltPart = "";
  18. String shoesPart = "";
  19. if (look.toLowerCase().contains("hr"))
  20. {
  21. hairPart = look.split("hr")[1].split(".")[0];
  22. }
  23. if (look.toLowerCase().contains("hd"))
  24. {
  25. headPart = look.split("hd")[1].split(".")[0];
  26. }
  27. if (look.toLowerCase().contains("he"))
  28. {
  29. headAccPart = look.split("he")[1].split(".")[0];
  30. }
  31. if (look.toLowerCase().contains("ha"))
  32. {
  33. hatPart = look.split("ha")[1].split(".")[0];
  34. }
  35. if (look.toLowerCase().contains("ea"))
  36. {
  37. glassesPart = look.split("ea")[1].split(".")[0];
  38. }
  39. if (look.toLowerCase().contains("fa"))
  40. {
  41. beardPart = look.split("fa")[1].split(".")[0];
  42. }
  43. if (look.toLowerCase().contains("ch"))
  44. {
  45. chestPart = look.split("ch")[1].split(".")[0];
  46. }
  47. if (look.toLowerCase().contains("cc"))
  48. {
  49. jacketPart = look.split("cc")[1].split(".")[0];
  50. }
  51. if (look.toLowerCase().contains("ca"))
  52. {
  53. necklacePart = look.split("ca")[1].split(".")[0];
  54. }
  55. if (look.toLowerCase().contains("lg"))
  56. {
  57. pantsPart = look.split("lg")[1].split(".")[0];
  58. }
  59. if (look.toLowerCase().contains("wa"))
  60. {
  61. beltPart = look.split("wa")[1].split(".")[0];
  62. }
  63. if (look.toLowerCase().contains("sh"))
  64. {
  65. shoesPart = look.split("sh")[1].split(".")[0];
  66. }
  67. 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 + ".";
  68. }

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

英文:

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:

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

Here is the line of code at the highest leve:

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

Whole method:

  1. public static String splitFigure(String look) {
  2. String hairPart = "";
  3. String headPart = "";
  4. String headAccPart = "";
  5. String hatPart = "";
  6. String glassesPart = "";
  7. String beardPart = "";
  8. String chestPart = "";
  9. String jacketPart = "";
  10. String necklacePart = "";
  11. String pantsPart = "";
  12. String beltPart = "";
  13. String shoesPart = "";
  14. if (look.toLowerCase().contains("hr"))
  15. {
  16. hairPart = look.split("hr")[1].split(".")[0];
  17. }
  18. if (look.toLowerCase().contains("hd"))
  19. {
  20. headPart = look.split("hd")[1].split(".")[0];
  21. }
  22. if (look.toLowerCase().contains("he"))
  23. {
  24. headAccPart = look.split("he")[1].split(".")[0];
  25. }
  26. if (look.toLowerCase().contains("ha"))
  27. {
  28. hatPart = look.split("ha")[1].split(".")[0];
  29. }
  30. if (look.toLowerCase().contains("ea"))
  31. {
  32. glassesPart = look.split("ea")[1].split(".")[0];
  33. }
  34. if (look.toLowerCase().contains("fa"))
  35. {
  36. beardPart = look.split("fa")[1].split(".")[0];
  37. }
  38. if (look.toLowerCase().contains("ch"))
  39. {
  40. chestPart = look.split("ch")[1].split(".")[0];
  41. }
  42. if (look.toLowerCase().contains("cc"))
  43. {
  44. jacketPart = look.split("cc")[1].split(".")[0];
  45. }
  46. if (look.toLowerCase().contains("ca"))
  47. {
  48. necklacePart = look.split("ca")[1].split(".")[0];
  49. }
  50. if (look.toLowerCase().contains("lg"))
  51. {
  52. pantsPart = look.split("lg")[1].split(".")[0];
  53. }
  54. if (look.toLowerCase().contains("wa"))
  55. {
  56. beltPart = look.split("wa")[1].split(".")[0];
  57. }
  58. if (look.toLowerCase().contains("sh"))
  59. {
  60. shoesPart = look.split("sh")[1].split(".")[0];
  61. }
  62. 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 + ".";
  63. }

Example of what I passed in:

  1. ch-255-110.hr-165-1394.hd-180-1380.lg-280-110

答案1

得分: 1

尝试使用这个替代:

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

使用以下代码进行检查:

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

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

英文:

Try using this instead:

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

Checked with this

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

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

答案2

得分: 0

  1. public static void main(String[] args) {
  2. String look = "ch-255-110.hr-165-1394.hd-180-1380.lg-280-110";
  3. splitFigure(look);
  4. System.out.println(look);
  5. }
  6. public static String splitFigure(String look) {
  7. String hairPart = "";
  8. String headPart = "";
  9. String headAccPart = "";
  10. String hatPart = "";
  11. String glassesPart = "";
  12. String beardPart = "";
  13. String chestPart = "";
  14. String jacketPart = "";
  15. String necklacePart = "";
  16. String pantsPart = "";
  17. String beltPart = "";
  18. String shoesPart = "";
  19. if (look.toLowerCase().contains("hr")) {
  20. hairPart = look.split("hr")[1].split("\\.")[0];
  21. }
  22. if (look.toLowerCase().contains("hd")) {
  23. headPart = look.split("hd")[1].split("\\.")[0];
  24. }
  25. if (look.toLowerCase().contains("he")) {
  26. headAccPart = look.split("he")[1].split("\\.")[0];
  27. }
  28. if (look.toLowerCase().contains("ha")) {
  29. hatPart = look.split("ha")[1].split("\\.")[0];
  30. }
  31. if (look.toLowerCase().contains("ea")) {
  32. glassesPart = look.split("ea")[1].split("\\.")[0];
  33. }
  34. if (look.toLowerCase().contains("fa")) {
  35. beardPart = look.split("fa")[1].split("\\.")[0];
  36. }
  37. if (look.toLowerCase().contains("ch")) {
  38. chestPart = look.split("ch")[1].split("\\.")[0];
  39. }
  40. if (look.toLowerCase().contains("cc")) {
  41. jacketPart = look.split("cc")[1].split("\\.")[0];
  42. }
  43. if (look.toLowerCase().contains("ca")) {
  44. necklacePart = look.split("ca")[1].split("\\.")[0];
  45. }
  46. if (look.toLowerCase().contains("lg")) {
  47. pantsPart = look.split("lg")[1].split("\\.")[0];
  48. }
  49. if (look.toLowerCase().contains("wa")) {
  50. beltPart = look.split("wa")[1].split("\\.")[0];
  51. }
  52. if (look.toLowerCase().contains("sh")) {
  53. shoesPart = look.split("sh")[1].split("\\.")[0];
  54. }
  55. return "ha" + hatPart + "." + "he" + headAccPart + "." + "hr" + hairPart + "." +
  56. "hd" + headPart + "." + "ea" + glassesPart + "." + "fa" + beardPart + "." + "ch" +
  57. chestPart + "." + "cc" + jacketPart + "." + "ca" + necklacePart + "." + "lg" +
  58. pantsPart + "." + "wa" + beltPart + "." + "sh" + shoesPart + ".";
  59. }
英文:
  1. public static void main(String[] args) {
  2. String look = "ch-255-110.hr-165-1394.hd-180-1380.lg-280-110";
  3. splitFigure(look);
  4. System.out.println(look);
  5. }
  6. public static String splitFigure(String look) {
  7. String hairPart = "";
  8. String headPart = "";
  9. String headAccPart = "";
  10. String hatPart = "";
  11. String glassesPart = "";
  12. String beardPart = "";
  13. String chestPart = "";
  14. String jacketPart = "";
  15. String necklacePart = "";
  16. String pantsPart = "";
  17. String beltPart = "";
  18. String shoesPart = "";
  19. if (look.toLowerCase().contains("hr"))
  20. {
  21. hairPart = look.split("hr")[1].split("\\.")[0];
  22. }
  23. if (look.toLowerCase().contains("hd"))
  24. {
  25. headPart = look.split("hd")[1].split("\\.")[0];
  26. }
  27. if (look.toLowerCase().contains("he"))
  28. {
  29. headAccPart = look.split("he")[1].split("\\.")[0];
  30. }
  31. if (look.toLowerCase().contains("ha"))
  32. {
  33. hatPart = look.split("ha")[1].split("\\.")[0];
  34. }
  35. if (look.toLowerCase().contains("ea"))
  36. {
  37. glassesPart = look.split("ea")[1].split("\\.")[0];
  38. }
  39. if (look.toLowerCase().contains("fa"))
  40. {
  41. beardPart = look.split("fa")[1].split("\\.")[0];
  42. }
  43. if (look.toLowerCase().contains("ch"))
  44. {
  45. chestPart = look.split("ch")[1].split("\\.")[0];
  46. }
  47. if (look.toLowerCase().contains("cc"))
  48. {
  49. jacketPart = look.split("cc")[1].split("\\.")[0];
  50. }
  51. if (look.toLowerCase().contains("ca"))
  52. {
  53. necklacePart = look.split("ca")[1].split("\\.")[0];
  54. }
  55. if (look.toLowerCase().contains("lg"))
  56. {
  57. pantsPart = look.split("lg")[1].split("\\.")[0];
  58. }
  59. if (look.toLowerCase().contains("wa"))
  60. {
  61. beltPart = look.split("wa")[1].split("\\.")[0];
  62. }
  63. if (look.toLowerCase().contains("sh"))
  64. {
  65. shoesPart = look.split("sh")[1].split("\\.")[0];
  66. }
  67. return "ha" + hatPart + "." + "he" + headAccPart + "." + "hr" + hairPart + "." +
  68. "hd" + headPart + "." + "ea" + glassesPart + "." + "fa" + beardPart + "." + "ch" +
  69. chestPart + "." + "cc" + jacketPart + "." + "ca" + necklacePart + "." + "lg" +
  70. pantsPart + "." + "wa" + beltPart + "." + "sh" + shoesPart + ".";
  71. }

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:

确定