Search code examples
javasplitintegertrimparseint

Parsing String of scores into ArrayList<Integer>


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]);

Solution

  • 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.