Reputation: 621
I am new in Java. I have an object which contains below data
1002,USD,03/09/2019,1004,2,cref,,,,,,,,,
1002,USD,03/09/2019,1005,3,cref,,,,,,,,,
1002,USD,03/09/2019,1003,3,cref,,,,,,,,,
I used StringTokenizer to conver this in arraylist
List<String> elements = new ArrayList<String>();
StringTokenizer st = new StringTokenizer((String) object);
while(st.hasMoreTokens()) {
elements.add(st.nextToken());
}
Tokenizer convert this in below form
[1002,USD,03/09/2019,1004,2,cref,,,,,,,,,,
1002,USD,03/09/2019,1005,3,cref,,,,,,,,,,
1002,USD,03/09/2019,1003,3,cref,,,,,,,,,]
I want the third index of each line (1004,1005,1003) in a separate string or a separate array. Please advise or is there any other way to get the third index without using tokenizer.
java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
Upvotes: 1
Views: 287
Reputation: 426
To get the needed value (4th item) from each row, you can use String.split()
function:
List<String> elements = new ArrayList<String>();
StringTokenizer st = new StringTokenizer((String) object);
while(st.hasMoreTokens()) {
String[] row = st.nextToken().split(",");
if (row.length > 3) {
elements.add(row[3]);
}
}
Upvotes: 1
Reputation: 13
For a shorter version you can also use Streams:
Create two additonal ArrayList's to buffer the data
List<String> listToStoreData = new ArrayList<String>();
List<String[]> second = new ArrayList<String[]>();
Split each element by ',' into an Array and add them to the 'second' ArrayList
elements.stream().forEach(e -> second.add(e.toString().split(",")));
For each element of 'second', push the data at the third position of the array in to 'listToStoreData' ArrayList
second.stream().forEach(s -> listToStoreData.add(s[3]));
Next Convert the list to a String and store the string to a variable
String data = listToStoreData.toString();
Output of data = [1004, 1005, 1003]
Upvotes: 0
Reputation: 1217
String text = "yourtext";
String[] lines = text.split("\\r?\\n");
List<String> elements = new ArrayList<String>();
for(String line:lines) {
String[] words = line.split(",");
elements.add(words[3]);
}
Upvotes: 0
Reputation: 2304
You can use module to iterate over your elements
list and peek each time the ones that equals 0 :
for rank
as the index of element in a list
rank % 3 = 0
Upvotes: 0
Reputation: 5662
If you know that your separate elements always appear at at the 3rd index, then just count.
List<String> elements = new ArrayList<String>();
StringTokenizer st = new StringTokenizer((String) object);
List<String> separateList = new ArrayList<>(); // the list where you put your separate tokens.
int index=0; // start an index counter (at 0)
while(st.hasMoreTokens()) {
String token = st.nextToken(); // your token in a separat variable
elements.add(token); // adds every token to your list
if(index==3){ // once your index reaches 3 (4th element)
index=0; // reset the index to 0
separateListe.add(token); // and add the token to the separate list
}else{
index++; // for every other index, just count (index +1)
}
}
In general it is always easier to put everything in real "Lists" (not arrays, they are too rigid and not in Strings they don't perform good). That way you can handle your data and convert it to whatever you want once your data-handling is finished.
Upvotes: 0