1

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)

James Z
  • 12,209
  • 10
  • 24
  • 44
Muhammad Kazim
  • 611
  • 2
  • 11
  • 26

5 Answers5

1

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]);
    }
}
Edvins
  • 406
  • 5
  • 9
  • 1
    keep this in mind: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead." https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html – JavaMan Sep 04 '19 at 05:48
0

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.

GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • since it is the third index of "each line" you just have add a new if that resets the `index` variable to 0 when you hit the linebreak. – GameDroids Sep 03 '19 at 08:31
  • i think u didnt get my query.... i want (1004,1005,1003) in seperate string or array from data i have. – Muhammad Kazim Sep 03 '19 at 08:46
0

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
Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37
0
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]);
}
JavaMan
  • 1,142
  • 12
  • 22
0

For a shorter version you can also use Streams:

  1. Create two additonal ArrayList's to buffer the data

    List<String> listToStoreData = new ArrayList<String>();
    List<String[]> second = new ArrayList<String[]>();
    
  2. Split each element by ',' into an Array and add them to the 'second' ArrayList

    elements.stream().forEach(e ->  second.add(e.toString().split(",")));
    
  3. 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]));
    
  4. Next Convert the list to a String and store the string to a variable

    String data = listToStoreData.toString();
    

Output of data = [1004, 1005, 1003]

Michael29
  • 13
  • 2