Zain
Zain

Reputation: 5571

How to access the tokenized value in Java

I have tokenized string, I want to access the 1st,2nd,3rd value from it.

Can anyone look into this?

Thanks in advance!

Here is the code

String cssvalue = webDriver.findElement(By.id(elementId)).getCssValue("color");
String s = cssvalue;
StringTokenizer st = new StringTokenizer(s, ",() ");
while (st.hasMoreTokens()) {

    log.info("TOken: "+st.nextToken());
}

Upvotes: 1

Views: 216

Answers (3)

Gauthier Boaglio
Gauthier Boaglio

Reputation: 10262

Just for fun because I think storing .hasMoreToken values is pointless :

String cssvalue = webDriver.findElement(By.id(elementId)).getCssValue("color");
String [] s = cssvalue.split("rgb");
StringTokenizer st = new StringTokenizer(s[1], ",() ");
List<Integer> values = new ArrayList<Integer>();
List<Boolean> more = new ArrayList<Boolean>();
Boolean b;
while (b = st.hasMoreTokens()) {
  values.add(Integer.parseInt(st.nextToken()));
  more.add(b); 
  log.info("TOken: "+values.get(values.size()-1));
}

Upvotes: 0

ritesh
ritesh

Reputation: 917

This might help:

String cssvalue = webDriver.findElement(By.id(elementId)).getCssValue("color");
String [] s = cssvalue.split("rgb");
StringTokenizer st = new StringTokenizer(s[1], ",() ");
List<Integer> values = new ArrayList<Integer>();
int index=0;
while (st.hasMoreTokens()) {
  values.add(Integer.parseInt(st.nextToken()));
  log.info("TOken: "+values.get(index));
  index++;

}
Integer [] a = values.toArray(new Integer[3]);

Upvotes: 1

Gauthier Boaglio
Gauthier Boaglio

Reputation: 10262

Ok so you have to store the first 3 tokens :

String cssvalue = webDriver.findElement(By.id(elementId)).getCssValue("color");
String s = cssvalue;
StringTokenizer st = new StringTokenizer(s, ",() ");
ArrayList<String> rgb_lst = new ArrayList<String>();
while (st.hasMoreTokens()) {
    rgb_lst.add(st.nextToken());
    if (rgb_lst.size() == 3) break;
}

Or something similar.

Then you can get rgb_lst[0], rgb_lst[1] and rgb_lst[2].

Upvotes: 0

Related Questions