2015-11-04 77 views
0

有人可以让我知道如何删除下面代码的输出列表中显示的$字符?另外,我想在删除后添加所有元素的总和。请确实需要。

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); 
      } 

} 

输出:

[$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] 
+0

你尝试自己做任何事看起来这是现有的代码,您希望我们完成您的全部任务。 – f1sh

回答

0

如果所有元素都有一个 '$' 作为第一个字符,只是改变这一行来摆脱他们:

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

到:

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

要计算总和,请在循环中编码一个循环,或者在将其插入options数组的同时添加该值。

+0

谢谢弗兰:)它的工作。将包括一个循环求和并让你知道。 – Rahul

+0

Fran,我尝试了不同的方法,但没有得到逻辑来完成那些数组值的总和,因为它们是Java的新东西。请让我知道同样的东西。提前致谢 !! – Rahul

+0

@拉胡评论,http://stackoverflow.com/questions/4550662/how-do-you-find-the-sum-of-all-the-numbers-in-an-array-in-java –

0

将它添加到optionsList之前,只需删除$options阵列:

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

然后做了总结。

注意有很多选项可以做到这一点。

0

您可以在列表实现下面的逻辑 -

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; 
} 
0

工作代码:

                        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); 
     } 

}