I am just trying to split a string into two smaller strings. It seems like it would be easy but for some reason I cannot figure it out. An example of what I am trying to split is this:
AA
BB
DD
FF
TT
EE
SE
GR
These values are currently in an ArrayList and I would like them to split into two arraylists
A A
B B
D D
I tried this
prse = calls.get(t).split(""); //also tried prse = calls.get(t).split("", -1);
call1.add(prse[0]);
call2.add(prse[1]);
But it filled my arraylists with empty characters. (Printed []
[, ]
[, , ]
[, , , ]
[, , , , ])
So then I tried
String split = calls.get(t).substring(0,1); //This is 285 that is puking
String split2 = calls.get(t).substring(1,2);
System.out.println(split);
call1.add(split);
System.out.println("1: " + call1);
call2.add(split2);
System.out.println("2: " + call2);
Which works up to a point but then dies a horrible death:
1: [F, P, T, B, F, D, S, G, G, G, Z, S, Q, E, F, H, L, P, O, L, G, B, V, F, W, Q, T, H, G, E, R, T, Y, C, G, D, C, H, C, A]
2: [o, G, U, U, Y, Y, T, T, T, T, T, R, E, S, G, J, H, C, H, H, H, G, C, J, H, D, G, J, H, D, R, G, V, C, G, V, V, D, V, V]
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.substring(String.java:1946)
at print.Reader.ffy(Reader.java:285)
at print2.Print2.main(Print2.java:48)
Can anyone suggest a better way to do this (Or explain to me what I am doing wrong)?
Notes:
I would like to put the split letters into their own, separate ArrayList String (due to later usage.) so I would rather not charArray.
when u split with blank string => “”, the first string that u get is blank(“”).
so example : “AB” prse[0] = “”, prse[1]=”A” ,prse[2]=”B”