Only accept non-negative numbers Java -
else if (bb.equals("dollar") && cc.equals("pound")){ input = joptionpane.showinputdialog("what amount convert?:"); double aa = double.parsedouble(input); double output =(aa*rate); joptionpane.showmessagedialog(null, string.format("your amount is: $%.2f", output)); how make if enters negative number message popup saying invalid input?
i assume number you're checking see if it's positive defined variable aa? prefer using bigdecimal when comes dealing monetary values, in case do:
bigdecimal aa = null; try { aa = new bigdecimal(input); } catch (numberformatexception nfe) { //not numerical value, throw error } if (aa.compareto(bigdecimal.zero) <= 0) { //value negative }
Comments
Post a Comment