Java 如何删除'$';列表中的字符,并打印列表中所有元素的总和

Java 如何删除'$';列表中的字符,并打印列表中所有元素的总和,java,arrays,sum,Java,Arrays,Sum,有人能告诉我如何删除下面代码输出列表中显示的$字符吗?另外,我想在删除后添加所有元素的总和。请做必要的事 public static void HHDollarAmoutValidation(){ try{ int AAWithclientTablecount = webDriver.findElements(By.xpath("//*[@id='DataTables_Table_21']/tbody/tr")).size();

有人能告诉我如何删除下面代码输出列表中显示的
$
字符吗?另外,我想在删除后添加所有元素的总和。请做必要的事

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

}

如果所有元素的第一个字符都是“$”,只需更改这一行即可:

options[i-1]=webDriver.findElement(By.xpath(“/*[@id='DataTables\u Table\u 21']]/tbody/tr[“+i+”]]/td[8]”)

致:

options[i-1]=webDriver.findElement(By.xpath(“/*[@id='DataTables\u Table\u 21']]/tbody/tr[“+i+”]/td[8]”)


要计算总和,请编写循环代码或在将其插入
选项
数组的同时添加值。

只需从
选项
数组中删除
$
,然后再将其添加到
选项列表

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


注意:有许多选项可供选择。

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

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;
}
publicstaticvoidmain(字符串[]args){
列表l=新的LinkedList();
List r=新的LinkedList();
l、 加上(“$12”);
l、 加上(“$3.2”);
l、 加上(“$2.5”);
l、 添加(“4.5”);
for(字符串s:l){
如果(以“$”开头){
s=s。子串(1);
r、 添加(新的双精度);
}否则{
r、 添加(新的双精度);
}
}
系统输出println(r);
系统输出println(getSum(r));
}
私有静态双getSum(列表l){
双r=新双(0);
用于(双d:l){
r=r+d;
}
返回r;
}
工作代码:

                                                                                                   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);
      }
publicstaticvoid HHDollarAmoutValidation(){
试试{
int AAWithclientTablecount=webDriver.findElements(By.xpath(“//*[@id='DataTables\u Table\u 21']/tbody/tr”).size();
System.out.println(AAWithclientTablecount);
字符串[]选项=新字符串[AAWithclientTablecount];
列表选项列表=空;

对于(inti=1;我自己试过了吗?看起来这是现有代码,您希望我们完成您的全部任务。谢谢Fran:)它成功了。将包括一个求和循环,并让您知道。Fran,我尝试了不同的方法,但没有得到逻辑来对这些数组值求和,因为我是Java新手。您能让我知道同样的情况吗?提前谢谢!!@Raghu回顾一下
                                                                                                   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);
      }