0

Can someone please let me know how to remove the $ character displayed in the output list of the below code? Also, I want to add the sum of all the elements after removing. Please do needful.

public static void HHDollarAmoutValidation(){
       try{              
            int AAWithclientTablecount = webDriver.findElements(By.xpath("//*[@id='DataTables_Table_21']/tbody/tr")).size();               
            System.out.println(AAWithclientTablecount);
            String[] options=new String[AAWithclientTablecount];
            List<String> optionsList=null;
            for (int i=1; i<=AAWithclientTablecount; i++)
            {
              options[i-1] = webDriver.findElement(By.xpath("//*[@id='DataTables_Table_21']/tbody/tr["+ i + "]/td[8]")).getText();
              System.out.println(options[i-1]);
              optionsList = Arrays.asList(options);

            }
               System.out.println(optionsList);
          }

          catch(Exception e){
               System.out.println(e);
          }

}

Output:

[$3,171,349.51, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00, $0.00]
justcurious
  • 839
  • 3
  • 12
  • 29
Rahul
  • 759
  • 3
  • 21
  • 43
  • Did you try anything yourself? It looks like this is existing code and you want us to do your whole assignment. – f1sh Nov 04 '15 at 10:52

4 Answers4

0

If all elements have a '$' as first char, just change this line to get rid of them:

options[i-1] = webDriver.findElement(By.xpath("//*[@id='DataTables_Table_21']/tbody/tr["+ i + "]/td[8]")).getText();

to:

options[i-1] = webDriver.findElement(By.xpath("//*[@id='DataTables_Table_21']/tbody/tr["+ i + "]/td[8]")).getText().substring(1);

For calculating the sum, code a loop or add the value at the same time that you insert it in the options array.

Fran Montero
  • 1,679
  • 12
  • 24
  • Thanks Fran :) It worked. Will include a loop for summation and let you know. – Rahul Nov 04 '15 at 11:57
  • Fran, I tried different ways but not getting the logic to do sum of those array values as am new to Java.Could you please let me know the same. Thanks in advance !! – Rahul Nov 04 '15 at 12:50
  • @Raghu review that http://stackoverflow.com/questions/4550662/how-do-you-find-the-sum-of-all-the-numbers-in-an-array-in-java – Fran Montero Nov 04 '15 at 12:59
0

Just remove the $ from options array before adding it to the optionsList:

options[i - 1] = options[i - 1].replace("$", ""); //add this line in the for loop

Then do the summation.

Note: There are many options to do this.

Husam
  • 2,069
  • 3
  • 19
  • 29
0

You can implement following logic in List -

public static void main(String[] args) {
    List<String> l = new LinkedList<String>();
    List<Double> r = new LinkedList<Double>();
    l.add("$12");
    l.add("$3.2");
    l.add("$2.5");
    l.add("4.5");

    for (String s: l) {
        if (s.startsWith("$")) {
            s = s.substring(1);
            r.add(new Double(s));
        } else {
            r.add(new Double(s));
        }
    }

    System.out.println(r);

    System.out.println(getSum(r));
}

private static Double getSum(List<Double> l) {
    Double r = new Double(0);

    for(Double d : l) {
        r = r + d;
    }

    return r;
}
Saurabh
  • 2,384
  • 6
  • 37
  • 52
0

Working Code :

                                                                                                   public static void HHDollarAmoutValidation(){
   try{              
        int AAWithclientTablecount = webDriver.findElements(By.xpath("//*[@id='DataTables_Table_21']/tbody/tr")).size();               
        System.out.println(AAWithclientTablecount);
        String[] options=new String[AAWithclientTablecount];
        List<String> optionsList=null;
       for (int i=1; i<=AAWithclientTablecount; i++)
            {
                String Vstring = webDriver.findElement(By.xpath("//*[@id='DataTables_Table_21']/tbody/tr["+ i + "]/td[8]")).getText().replace(",", "").substring(1).trim();

                System.out.println(Vstring+ " (at row: "+i+")");

                sumIt = sumIt + Double.parseDouble(Vstring);
            }

                System.out.println(sumIt);

       }

      catch(Exception e){
           System.out.println(e);
      }

}

Rahul
  • 759
  • 3
  • 21
  • 43