java - How do I fix the error of NumberFormatException everytime I run the program? -


i have create love calculator object computer science class. however, every time compile , run program keep ending error:

java.lang.numberformatexception:

for input string: "70.78%" (in sun.misc.floatingdecimal)

my code method:

public double calculate() {     double value1;     double value2;     double sqrt1;     double sqrt2;     double relationship;     sqrt1 = math.sqrt(name1.length());     sqrt2 = math.sqrt(name2.length());     value1 = (math.pow(name1.length(), 3)/(math.random()+0.1))* sqrt1;     value2 = (math.pow(name2.length(), 3)/(math.random()+0.1))* sqrt2;      if(value1 > value2)     {         relationship = value2 / value1;     }     else     {         relationship = value1 / value2;     }     numberformat nf = numberformat.getpercentinstance();     nf.setminimumfractiondigits(2);     return double.parsedouble(nf.format(relationship)); } 

i attempted convert float. tried separate declaring , initializing double variable , returning instead didn't work. looked solutions , said use try , catch don't understand how work (since began class , beginner).

how use try , catch situation?

numberformat meant create human readable string. 70.78% isn't number. 70.78 is, percent sign, it's string. seems you're trying use number formatting functionality round number. this question has suggestions how round number , keep number.

to answer other question, proper way use try/catch this:

 double result;  try{    result = double.parsedouble(nf.format(relationship));  }catch(numberformatexception e){    e.printstacktrace();    result = 0.0;  }  return result; 

but thing cause program not crash , you'll 0.0 returned calculate() method. instead need fix source of exception.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -