Reputation: 778
I have a String that I want to split from a Stringbuffer and save it into an arraylist of object.
I have a string of data that looks something like this:
001>10102011>874837>Y>Y>TEST001>No\\002>11102011>8743834>Y>Y>null>No\\
The \ will represent to save to a new object. How do I save this string into List listsaveobj?
My sample save object is
public class SaveObject{
private String obj1;
private String obj2;
private String obj3;
private String obj4;
private String obj5;
private String obj6;
private String obj7;
//getters and setters
}
I am stuck with
StringTokenizer st= new StringTokenizer(sb1.toString(),"\\\\");
int count = 0;
int total= st.countTokens();//count no of rows of data
System.out.println(total);
while(count<total){
while(st.hasMoreElements()){
tmpList.add(st.nextToken());
count++;
}
}
System.out.println(tmpList.size());
for(int j = 0; j<tmpList.size(); j++){
String str = tmpList.get(j);
StringTokenizer st2 = new StringTokenizer(str, ">");
while (st2.hasMoreElements()){
System.out.println(st2.nextToken());
// is it possible to save into ArrayList of objects here?
// I can see that the String can split into
// 001
// 10102011
// 874873
// Y
// Y
// TEST001
// No etc
/* using st.nextToken but I don't know how to continue from here. Pls help!! Thanks. I want to do something like
obj = new Object();
obj.saveObj1(...);
obj.saveObj2(...);
obj.saveObj3(...);
obj.saveObj4(...);
obj.saveObj5(...);
obj.saveObj6(...);
obj.saveObj7(...);
listsaveobj.add(obj); but it's not possible? */
}
}
Upvotes: 0
Views: 3054
Reputation: 2363
Here is a better way to extract the data:
String s = "001>10102011>874837>Y>Y>TEST001>No\\\\002>11102011>8743834>Y>Y>null>No\\\\";
String[] rows = s.split("\\\\");
for (String r : rows) {
String[] cols = r.split(">");
for (String c : cols) {
System.out.print(c + "\t");
}
System.out.println();
}
Upvotes: 1