Hello guys I'm having an error when executing this code it works perfectly in my computer but when I sent the application to my friend to try it it caused a number format exception, is there a problem with the code? if so why is it working in my computer ps: I tried the application in another computer and it works
double total = 0;
//here there is a for loop changing the total value
//but in this case (when the application starts) the loop condition will be false
//so the total will stay 0
String totalS = String.format("%.2f", total);
if (totalS.endsWith(".00")) {
totalS = totalS.substring(0,totalS.length()-3);
}
lblDa.setText(new BigDecimal(totalS).toPlainString() +" DA");
edit: here is the error message
java.lang.NumberFormatException: Character , is neither a decimal digit number, decimal point, nor "e" notation exponential mark.
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:519)
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:402)
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:835)
at invpack.MainFrame.updateTotal(MainFrame.java:3052)
at invpack.MainFrame.initialize(MainFrame.java:557)
at invpack.MainFrame.<init>(MainFrame.java:185)
at invpack.MainFrame$1.run(MainFrame.java:171)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
The String.format
method you are using looks up details about the number format from user preferences: it may use "," instead of "." for the decimal separator, it may even use digits other than 0-9. The rest of your program assumes that the generated string uses ASCII digits and "." for decimal separator.
When you need String.format
to generate the number in a specific style, pass in a fixed Locale argument:
String totalS = String.format(Locale.ROOT, "%.2f", total);
On the other hand, it looks like all of this code is there only so that you don't display trailing zeros. There's a more straightforward way to achieve that: use a number format that makes trailing zeros optional.
DecimalFormat formatter = new DecimalFormat("0.##");
lblDa.setText(formatter.format(total) +" DA");