1

I'm trying to parse a string into an array of Integers. The string represents a bunch of game scores, which I then want to split into arrays for each individual player's scores. I'm using split and trim, and then iterating through each score and putting it into an ArrayList<Integer> using Integer.parseInt()

It seems as though it goes through the first loop fine, and then crashes on the second iteration. Here is my code and logCat file. I've added some println statements so the reader can see what I'm working with.

Code

 //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)

where line450 is

int score1 = (Integer) Integer.parseInt(individualScores[0]);
Code Vader
  • 739
  • 3
  • 9
  • 26
  • try using trim after invoking replaceAll, because, going by the log the string that is being parsed into integer has leading white spaces and that is indicated as " 9" in the NumberFormatException – Prudhvi Konda Aug 30 '15 at 06:35

7 Answers7

1

You can use:

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

You may want to handle null strings though.

Amila
  • 5,195
  • 1
  • 27
  • 46
1

The problem is that you trim before removing characters that are not numbers (consider for example the case where individualScores[0] is _ 5). Swap the two lines, like this:

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

By the way, [^a-zA-Z0-9]+$ will also keep letters and I am not sure you want to keep them. You should use the regex [^0-9]+$ to only keep numbers.

Also, in the following line:

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

the cast to Integer is useless: Integer.parseInt returns an int.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

So the problem is: java.lang.NumberFormatException: Invalid int: " 9"

Why it happened,

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

individualScores[0] was " 9"

To avoid it use,

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


Also, the below line is incorrect(logically wrong),

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.

Note that it returns a copy after removing white spaces.

Read more about Immutability of Strings in Java

Community
  • 1
  • 1
Ghazanfar
  • 1,419
  • 13
  • 21
1

Strings are immutable by nature, so your call to trim wont change the character within it unless you assign it back to same/another String element. So you should change your code from:

individualScores[0].trim();

to:

individualScores[0] = individualScores[0].trim();//store trimmed version of String back
SMA
  • 36,381
  • 8
  • 49
  • 73
1

Since String is immutable, the trim() function returns a new String. That is why your trim doesn't work. You should modify your code to accept the return value as such:

 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]+$", "");
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
0

In this line,

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

individualScores[0] was " 9" //have extra spaces

To avoid it use,

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

Hope this will helpful

Vignesh Shiv
  • 1,129
  • 7
  • 22
0
for(int z = 0; z < stringArray.length; z++)
{
 stringArray[z] =  stringArray[z].replaceAll("\\s+","");
 System.out.println("String Array "+z+": >"+stringArray[z]+"<");
}
karim mohsen
  • 2,164
  • 1
  • 15
  • 19