gpt4 book ai didi

java - 获取列中的总金额

转载 作者:行者123 更新时间:2023-11-30 07:05:34 27 4
gpt4 key购买 nike

我有下面的方法

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

}

下面是类型

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

在模型中我有以下内容

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

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

当我尝试对总计求和时,我只得到第一列作为总计。查看下面的日志

2222 : 460.0
not a number

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

查看下面的堆栈跟踪

信息:已安装的 AtmosphereInterceptor 跟踪消息大小拦截器使用 |优先级为 BEFORE_DEFAULT

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

最佳答案

首先,您要在循环内设置列页脚,您的以下代码应该在循环外,

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

其次,您正在转换为 String 并使用 toString 以及使用其中之一,在第二次迭代期间,您获得的值无法解析 加倍,这样你得到的不是一个数字,你正在打印 int catch block ,所以要获得有关确切问题的更多信息,你应该使用 e。 printStackTrace(); 在 catch block 中。

2222 : 460.0
not a number

所以,从堆栈跟踪中我们可以看到您从 Amount 属性中获得的值是

2,336.00
^

由于 ,String 中,所以这个值不能被解析为 double 简单的解决方案可以是使用 #replace并且你最好在解析之前使用空检查,这样你就可以避免空指针 excaption。

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

关于java - 获取列中的总金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26527382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com