简体   繁体   English

在列中获取总金额

[英]Getting total amount in a column

I have the below method 我有以下方法

 public void footerSet() {

    // Calculate total sum
    double totalSum = 0.0;
    // double totalMargin = 0.0;
    for (int i = 0; i < ic.size(); i++) {
        Item item = ic.getItem(ic.getIdByIndex(i));

        try {

            double valuetotal = Double.parseDouble((String) item
                    .getItemProperty("Amount").getValue());

            totalSum += valuetotal;
            System.out.println(">>>2222 : " + totalSum);

            // System.out.println(">>>2222 : " + totalSum);
        } catch (NumberFormatException e) {
            // System.out.println("not a number");
            e.printStackTrace();
        }
    }
    ProductTableGlobal.setColumnFooter("Outlet", "Total");
    ProductTableGlobal.setColumnFooter("Amount", String.valueOf(totalSum));

}

and the type is below 并且类型在下面

    icLoaded.addContainerProperty("Outlet", String.class, null);
    icLoaded.addContainerProperty("Amount", String.class, null);

and in the model i have the below 在模型中我有以下内容

     //NumberFormat numberFormat = new DecimalFormat("#,###.00");

      double totalAmounts = totalAmount;
     passTable.getContainerProperty(UID, "Amount").setValue(
                numberFormat.format((totalAmounts)));

When i try to sum up the totals i only get the first column as the total. 当我尝试对总数求和时,我仅将第一列作为总数。 See below logs 见下面的日志

2222 : 460.0 2222:460.0
not a number 不是数字

>>>2222 : 460.0
>>>2222 : 460.0

See below stack trace 见下面的堆栈跟踪

INFO: Installed AtmosphereInterceptor Track Message Size Interceptor using | INFO:使用|安装AtmosphereInterceptor跟踪消息大小拦截器 with priority BEFORE_DEFAULT 优先级为BEFORE_DEFAULT

2222 : 460.0 2222:460.0

java.lang.NumberFormatException: For input string: "2,336.00"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
at java.lang.Double.parseDouble(Double.java:540)
at com.zocha.reports.TaxByOutlet.footerSet(TaxByOutlet.java:280)
at com.zocha.reports.TaxByOutlet.OutletSalesView(TaxByOutlet.java:84)
at com.zocha.reports.TaxByOutlet.enter(TaxByOutlet.java:62)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:571)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:526)
at valo.ValoThemeUI$5.buttonClick(ValoThemeUI.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:978)
at com.vaadin.ui.Button.fireClick(Button.java:393)
at com.vaadin.ui.Button$1.click(Button.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:287)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:180)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:93)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

>>>2222 : 460.0
>>>2222 : 460.0

Note: 2,336.00 is the second value that needs to be added and so on

First of all you are setting column footer inside loop your following code should be outside the loop, 首先,您需要在循环内设置列脚,以下代码应位于循环外,

ProductTableGlobal.setColumnFooter("Outlet", "Total");
ProductTableGlobal.setColumnFooter("Amount",String.valueOf(totalSum));

Secondly you are casting to String and using toString as well use one of them,during your second iteration value you are getting can not be parsed to double so that you got not a number which you are printing int catch block so to get more info about exact problem for that you should use e.printStackTrace(); 其次,您要强制转换为String并使用toString以及其中之一,在第二个迭代值期间,您将无法解析为双精度值 ,因此,您将获得的不是打印int catch的数字 ,从而获取有关确切的问题是您应该使用e.printStackTrace(); in catch block. 在捕获块中。

2222 : 460.0
not a number

SO ,from stacktrace we can see that the value you are getting from Amount property is 因此,从stacktrace中我们可以看到,您从Amount属性获得的值是

2,336.00
 ^

So this value can not be parsed to double due to , in String the easy solution can be the use of #replace and you better use null check before parsing so you can avoid null pointer excaption. 因此,由于不能将此值解析为double ,String ,简单的解决方案是使用#replace并且最好在解析之前使用null检查,这样可以避免对null指针的解释。

double valuetotal=0.0;
if(item!=null && item.getValue()!=null){
  String tempString = item.getItemProperty("Amount")
                          .getValue()
                          .toString()
                          .replace(",","");
  valuetotal=Double.parseDouble(tempString);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM