0

I'm trying to split a string to get the values in sets, for example I have this string:

215018,738,25094548 47137,77,1479107 80595,70,740157 38834,90,5419316 59382,82,2431554 865303,1,4 158088,44,56662 139671,61,325746 530224,5,410 783,99,14400482 268478,20,4981 577948,1,30 684122,1,40 222912,37,27987 598052,1,18 614235,1,40 69690,48,85643 235186,32,17801 329817,14,2179 118561,50,102391 380170,1,0 376338,1,0 374930,1,0 335953,1,0 -1,-1 -1,-1 -1,-1

I need to split this string by the spaces so that I end up with this:

215018,738,25094548

Next I split by commas and insert into an arraylist of a class:

ArrayList<SomeClass> someClass = new SomeClass<SomeClass>();
someClass.add(new SomeClass("215018", "738", "25094548"));

I need to do this for each value in the first string. As well I need to be able to ignore the '-1's at the end of the string.

I'm having having trouble understanding the logic and code behind doing this task.

Any help is appreciated :)

Jon Perron
  • 83
  • 9
  • 3
    You could perform the task exactly as you had described, ie, use `split()` on the `String` variable by `','` and then iterate over those values and check whether they are `>0` (ie, eliminating the `-1` values) and add them into your ArrayList as need be. – Anthony Forloney Jan 15 '15 at 21:25
  • So I split them by ",". They are stored in a List. How do I loop through the list to add them to the someClass arraylist? – Jon Perron Jan 15 '15 at 21:33

2 Answers2

1

Here is some dry-run code:

public class Split
{
    public static void main (String[] args) {
      String SPACE_K = " ";
      String SEPARATOR_K = ",";

      String input = "15018,738,25094548 47137,77,1479107 80595,70,740157 38834,90,5419316 59382,82,2431554 865303,1,4 158088,44,56662 139671,61,325746 530224,5,410 783,99,14400482 268478,20,4981 577948,1,30 684122,1,40 222912,37,27987 598052,1,18 614235,1,40 69690,48,85643 235186,32,17801 329817,14,2179 118561,50,102391 380170,1,0 376338,1,0 374930,1,0 335953,1,0";
      String[] numbers = input.split(SPACE_K);
      for (int i=0; i<numbers.length; i++) {
        String number = numbers[i];
        String[] values = number.split("\\"+SEPARATOR_K );
        if (values!=null && values.length==3) {
            Skill skill = new Skill(values[0], values[1], values[2]);
            System.out.println("\nCreated new object "+ skill.toString());
        }
      }
    }

    public static class Skill {
        private final String val1, val2, val3;

        public Skill(String in1, String in2, String in3)  {
          val1 = in1; val2 = in2; val3 = in3;
        }

        public String toString() {
          return "Skill ("+ val1 + ", " + val2 + ", " + val3 + ")";
        }        
    }
}
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • Looking this over now. – Jon Perron Jan 15 '15 at 21:56
  • This is exactly what I am looking for! Works flawless with a couple small changes to fit my code better. Thank you very much! – Jon Perron Jan 15 '15 at 22:05
  • Alright quickly while your here, I have an enumerator in the Skill class along with the other 3 values, how do I cycle through each value of the enumerator? `skill.add(new Skill(Skill.skill.OVERALL, values[0], values[1], values[2]));` – Jon Perron Jan 15 '15 at 22:07
  • Lets assume your enum is CardColor: List allCardColors = Arrays.asList(CardColor.values()); – MaxZoom Jan 15 '15 at 22:11
  • oh that makes sense! I didnt realize you could grab enumerator values like that, thanks again! – Jon Perron Jan 15 '15 at 22:13
0

Here's some pseudocode for you:

Separate it into sections, using a " " character with this function, public String[] split(String regex), like so

String[] splitBySpace = yourString.split(" "); 

This allows you to split using the spaces into arrays. X is holding a bunch of strings "x,y,z,a,b,c (etc) ". Then, call the same function again.

String[] splitbyComma = splitBySpace[i].split(",");  // this will split using the commas, run it in a for loop and save it somewhere

Now y holds array filled with single values. Add to the array, or arraylist or whatever list you choose to use at the end.

With regard to the "-1"s at the end of your string; just don't use the last 3 indexes of the x array (since there will be 3 copies of "-1,-1" inside x at the very end.

To add to the array list (assume list is your ArrayList):

for (int i = 0; i < /* your end condition */; i++) {
    String[] splitByComma = splitBySpace[i].split(",");
    list.add(new someObj(splitByComma[0], splitByComma[1], splitByComma[2]));
}
Aify
  • 3,543
  • 3
  • 24
  • 43
  • `String[] splitBySpace = line1.split(" "); String[] splitByComma = splitBySpace.split(",");` "Cannot invoke split(string) on the array type String[]" Am I doing something wrong? – Jon Perron Jan 15 '15 at 21:36
  • My bad, I missed the index[i], note the comment where i said you have to run it inside a for loop to split each thing in the splitBySpace array. I updated my answer to reflect your errors. – Aify Jan 15 '15 at 21:38
  • Alright I got them split by comma now, how do I insert them in to ArrayList of my class in pairs of 3? – Jon Perron Jan 15 '15 at 21:48