我试图将一个字符串解析为一个整数数组。这个字符串代表了一堆游戏分数,然后我想把它们分割成数组来表示每个玩家的分数。我使用split
和trim
,然后迭代每个分数,然后使用Integer.parseInt()
将其放入ArrayList<Integer>
中
它看起来好像通过第一个循环很好,然后在第二次迭代中崩溃。这是我的代码和logCat文件。我添加了一些println语句,这样读者就可以看到我正在处理的内容。
码
//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)
line450在哪里
int score1 = (Integer) Integer.parseInt(individualScores[0]);
发布于 2015-08-30 06:28:27
问题是,在删除不是数字的字符之前,您需要进行修剪(例如,考虑individualScores[0]
是_ 5
的情况)。交换这两行,如下所示:
individualScores[0] = individualScores[0].replaceAll("[^a-zA-Z0-9]+$", "");
individualScores[0] = individualScores[0].trim();
顺便说一句,[^a-zA-Z0-9]+$
也会保留信件,我不确定你是否愿意保留它们。您应该使用regex [^0-9]+$
只保留数字。
此外,在以下一行:
int score1 = (Integer) Integer.parseInt(individualScores[0]);
对Integer
的转换是无用的:Integer.parseInt
返回一个int
。
发布于 2015-08-30 06:27:00
您可以使用:
int score1 = (Integer) Integer.parseInt(individualScores[0].trim());
不过,您可能需要处理null
字符串。
发布于 2015-08-30 06:29:08
字符串本质上是不可变的,因此,除非您将字符串重新分配给相同/另一个字符串元素,否则您对trim的调用不会改变其中的字符。因此,您应该将代码更改为:
individualScores[0].trim();
至:
individualScores[0] = individualScores[0].trim();//store trimmed version of String back
https://stackoverflow.com/questions/32293846
复制相似问题