gpt4 book ai didi

Java 分割字符串的空部分

转载 作者:行者123 更新时间:2023-11-30 03:19:50 24 4
gpt4 key购买 nike

我已经分割了字符串。可以有1个部分,也可以有多个部分。我想获得最多 100 个第一部分,但其中很少有可能不存在。当部分没有值(value)时,我在尝试调试应用程序时遇到错误。这是我尝试做的,但它不起作用:

        String[] a = null;
a = out.split("\\{");
String c1 = "0";String c2 = "0";String c3 = "0";String c4 = "0";String c5 = "0";String c6 = "0";String c7 = "0";String c8 = "0";String c9 = "0";String c10 = "0";
String c11 = "0";String c12 = "0";String c13 = "0";String c14 = "0";String c15 = "0";String c16 = "0";String c17 = "0";String c18 = "0";String c19 = "0";String c20 = "0";
String c21 = "0";String c22 = "0";String c23 = "0";String c24 = "0";String c25 = "0";String c26 = "0";String c27 = "0";String c28 = "0";String c29 = "0";String c30 = "0";
String c31 = "0";String c32 = "0";String c33 = "0";String c34 = "0";String c35 = "0";String c36 = "0";String c37 = "0";String c38 = "0";String c39 = "0";String c40 = "0";
String c41 = "0";String c42 = "0";String c43 = "0";String c44 = "0";String c45 = "0";String c46 = "0";String c47 = "0";String c48 = "0";String c49 = "0";String c50 = "0";
String c51 = "0";String c52 = "0";String c53 = "0";String c54 = "0";String c55 = "0";String c56 = "0";String c57 = "0";String c58 = "0";String c59 = "0";String c60 = "0";
String c61 = "0";String c62 = "0";String c63 = "0";String c64 = "0";String c65 = "0";String c66 = "0";String c67 = "0";String c68 = "0";String c69 = "0";String c70 = "0";
String c71 = "0";String c72 = "0";String c73 = "0";String c74 = "0";String c75 = "0";String c76 = "0";String c77 = "0";String c78 = "0";String c79 = "0";String c80 = "0";
String c81 = "0";String c82 = "0";String c83 = "0";String c84 = "0";String c85 = "0";String c86 = "0";String c87 = "0";String c88 = "0";String c89 = "0";String c90 = "0";
String c91 = "0";String c92 = "0";String c93 = "0";String c94 = "0";String c95 = "0";String c96 = "0";String c97 = "0";String c98 = "0";String c99 = "0";String c100 = "0";
if(!a[2].isEmpty()){
String[] b1 = a[2].split(",");c1 = b1[1].split(":")[1];
}
if(!a[3].isEmpty()){
String[] b2 = a[3].split(",");c2 = b2[1].split(":")[1];
}
if(!a[4].isEmpty()){
String[] b3 = a[4].split(",");c3 = b3[1].split(":")[1];
}
if(!a[5].isEmpty()){
String[] b4 = a[5].split(",");c4 = b4[1].split(":")[1];
}
if(!a[6].isEmpty()){
String[] b5 = a[6].split(",");c5 = b5[1].split(":")[1];
}
if(!a[7].isEmpty()){
String[] b6 = a[7].split(",");c6 = b6[1].split(":")[1];
}

到目前为止,只有 a[2] 和 a[3] 有一些值,a[4] 应该为空。但看起来好像没有……这是一个错误:

java.lang.ArrayIndexOutOfBoundsException: 4
at someapp.stan(sapp.java:86)
at someapp.<init>(sapp.java:417)
at someapp.run(sapp.java:39)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$300(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是第 39 行

frame.getContentPane().add(new TestPane());

第 417 行(stan(id) 是公共(public)字符串,其中是我的分割字符串)

stant = stan(id);

第 86 行

if(!a[4].isEmpty()){

最佳答案

您应该检查是否

a.length < 4 

现在您已经调用了一个不存在的项目,请改为执行

for(int i=0;i<a.length;i++) // will never get an IndexOutOfBounds
a[i];// <-- do something....

关于Java 分割字符串的空部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31584825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com