将分数字符串解析为 ArrayList<Integer>

Parsing String of scores into ArrayList<Integer>

我正在尝试将字符串解析为整数数组。该字符串表示一堆游戏分数,然后我想将其拆分为每个玩家分数的数组。我正在使用 splittrim,然后遍历每个分数并使用 Integer.parseInt()

将其放入 ArrayList<Integer>

它似乎顺利通过了第一个循环,然后在第二次迭代时崩溃了。这是我的代码和 logCat 文件。我添加了一些 println 语句,因此 reader 可以看到我正在使用什么。

代码

 //PARSE SCORES INTO ARRAY
 String scores = result.getScores();

 System.out.println("Scores #1: "+scores);

 scores = scores.replaceAll("[^a-zA-Z0-9]+$", "");

 System.out.println("Scores #2: "+scores);

 String[] stringArray = scores.split(",");

 System.out.println("StringArray Length: "+stringArray.length);

 for(int z = 0; z < stringArray.length; z++)
 {
     System.out.println("String Array "+z+": >"+stringArray[z]+"<");
 }


 ArrayList<Integer> scoresA = new ArrayList<Integer>();
 ArrayList<Integer> scoresB = new ArrayList<Integer>();  

 for(int x = 0; x < stringArray.length-1; x++)
 {
     String[] individualScores = stringArray[x].split("-");

     individualScores[0].trim();
     individualScores[0] = individualScores[0].replaceAll("[^a-zA-Z0-9]+$", "");
     individualScores[1].trim();
     individualScores[1] = individualScores[1].replaceAll("[^a-zA-Z0-9]+$", "");



     int score1 = (Integer) Integer.parseInt(individualScores[0]);
     int score2 = (Integer) Integer.parseInt(individualScores[1]);

     System.out.println("Score1 :"+score1);
     System.out.println("Score2 :"+score2);

     if(team == SelectedTeam.TeamA)
     {
         scoresA.add(score1);
         scoresB.add(score2);
     }
     else
     {
         scoresB.add(score1);
         scoresA.add(score2);
     }
 }

LogCat

08-30 08:12:24.731: I/System.out(31452): Scores #1: 9-4,    9-0,    9-5,    
08-30 08:12:24.734: I/System.out(31452): Scores #2: 9-4,    9-0,    9-5
08-30 08:12:24.734: I/System.out(31452): StringArray Length: 3
08-30 08:12:24.734: I/System.out(31452): String Array 0: >9-4<
08-30 08:12:24.734: I/System.out(31452): String Array 1: >  9-0<
08-30 08:12:24.734: I/System.out(31452): String Array 2: >  9-5<
08-30 08:12:24.734: I/System.out(31452): Score1 :9
08-30 08:12:24.734: I/System.out(31452): Score2 :4
08-30 08:12:24.743: D/AndroidRuntime(31452): Shutting down VM
08-30 08:12:24.743: D/AndroidRuntime(31452): --------- beginning of crash
08-30 08:12:24.754: E/AndroidRuntime(31452): FATAL EXCEPTION: main
08-30 08:12:24.754: E/AndroidRuntime(31452): Process: com.squashbot, PID: 31452
08-30 08:12:24.754: E/AndroidRuntime(31452): java.lang.NumberFormatException: Invalid int: "    9"
08-30 08:12:24.754: E/AndroidRuntime(31452):    at java.lang.Integer.invalidInt(Integer.java:138)
08-30 08:12:24.754: E/AndroidRuntime(31452):    at java.lang.Integer.parse(Integer.java:410)
08-30 08:12:24.754: E/AndroidRuntime(31452):    at java.lang.Integer.parseInt(Integer.java:367)
08-30 08:12:24.754: E/AndroidRuntime(31452):    at java.lang.Integer.parseInt(Integer.java:334)
08-30 08:12:24.754: E/AndroidRuntime(31452):    at tournament_activities.MatchResult.PrepareScreenFromImportedResult(MatchResult.java:450)

第 450 行是

int score1 = (Integer) Integer.parseInt(individualScores[0]);

您可以使用:

int score1 = (Integer) Integer.parseInt(individualScores[0].trim());

虽然您可能想要处理 null 个字符串。

问题是您在删除非数字字符之前 trim(例如考虑 individualScores[0]_ 5 的情况)。交换两行,像这样:

individualScores[0] = individualScores[0].replaceAll("[^a-zA-Z0-9]+$", "");
individualScores[0] = individualScores[0].trim();

顺便说一下,[^a-zA-Z0-9]+$ 也会保留信件,但我不确定您是否要保留它们。您应该使用正则表达式 [^0-9]+$ 只保留数字。

此外,在以下行中:

int score1 = (Integer) Integer.parseInt(individualScores[0]);

转换为 Integer 没用:Integer.parseInt returns 一个 int.

所以问题是: java.lang.NumberFormatException: Invalid int: " 9"

为什么会这样,

int score1 = (Integer) Integer.parseInt(individualScores[0]);

individualScores[0]" 9"

为避免使用,

int score1 = (Integer) Integer.parseInt(individualScores[0].trim());


此外,下面这行是不正确的(逻辑错误),

individualScores[1].trim();

trim() returns:

A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

请注意,它 returns 在删除空格后 复制

阅读更多关于 Immutability of Strings in Java

字符串本质上是不可变的,因此您对 trim 的调用不会更改其中的字符,除非您将其分配回 same/another 字符串元素。所以你应该改变你的代码:

individualScores[0].trim();

至:

individualScores[0] = individualScores[0].trim();//store trimmed version of String back

由于 String 是不可变的,trim() 函数 returns 一个新的字符串。这就是为什么您的 trim 不起作用。您应该修改您的代码以接受 return 值:

 individualScores[0] = individualScores[0].trim();
 individualScores[0] = individualScores[0].replaceAll("[^a-zA-Z0-9]+$", "");
 individualScores[1] = individualScores[1].trim();
 individualScores[1] = individualScores[1].replaceAll("[^a-zA-Z0-9]+$", "");

这一行,

int score1 = (Integer) Integer.parseInt(individualScores[0]);

individualScores[0] 是“9”//有多余的空格

为避免使用,

int score1 = (Integer) Integer.parseInt(individualScores[0].trim());

希望这对您有所帮助

for(int z = 0; z < stringArray.length; z++)
{
 stringArray[z] =  stringArray[z].replaceAll("\s+","");
 System.out.println("String Array "+z+": >"+stringArray[z]+"<");
}