Link to home
Start Free TrialLog in
Avatar of fks2
fks2

asked on

Substraction on 2 int type number

Hi, i manage to convert 2 String to become integer. But when I want to substract these 2 number, there will be always thrown me
java.lang.NumberFormatException:

This is my code, any error?

int result=0;

String ab = javabean.getAb();
int abint =  new Integer(ab).intValue();

String pl = javabean.getPl();
int plint = new Integer(pl).intValue();

if (ab!="" && pl!=null) {
       result = pl - abint;
}


But this prompt me somehow error. WHat I hope to achieve is to substract the number (after return from the DB) equivalent to the String that I passed in.

:-)

Thankyhou
Avatar of paskal
paskal
Flag of Netherlands image

You're using the variable 'pl' in your substratcion instead of 'plint'. I guess that should make the difference.
Avatar of thanassis
thanassis

this is a better aproach

String ab = javabean.getAb();
String pl = javabean.getPl();
int plint = 0;
int abint = 0;
int result = 0;

if (ab != null && !ab.equals("") && pl!=null && !pl.equals("")) {
   plint = Integer.parseInt(pl);
   abint =  Integer.parseInt(ab);
   result = plint - abint;
}



String a="12";
String b="10";

int ai=Interger.parseInt(a);
int bi=Interger.parseInt(b);

int result=ai=bi;



a typo in r_a_j_e_s_h's code:
it should be
int result = ai - bi;

try this:
<%

int result=0;

try {
  int ab = Integer.parseInt(javabean.getAb());
  int pl = Integer.parseInt(javabean.getPl());
  result = pl - ab;
  out.println("Result is:" + result);
}  catch (NullPointerException npe) { %>
     One of the strings was null
<% }
   catch (NumberFormatException nfe) { %>
     One of the strings was not a number
<% } %>

This way you can track what went wrong when result isn't displayed.

CJ
Avatar of fks2

ASKER

CJ,

java.lang.NumberFormatException

my code is something like this,

int result=0;
try {

  String ab = javaBean.getAB();
     int latest_number = Integer.parseInt(daycount.getPo_number());
     int ori_number = Integer.parseInt(daycount.getLC_number());
 result=ori_number - latest_number;
 out.println("Result :" + result);

} catch (NullPointerException np) { np.printStackTrace(); }
 catch (NumberFormatException ne) { ne.printStackTrace(); }


How can I display the type of the variable of latest_number and ori_number?
Avatar of fks2

ASKER

guys, I think one of my "Int" type value is blank.

Is this help?

..
..
if (ori_number!="" && !ori_number.equals("") && latest_number!="" && !latest_number.equals("") ) {
result=ori_number - latest_number;
out.println("Result :" + result);
} else {
  out.println("Either one variable is blank");
}

..

but... error when I code like this,
how can I check for ub-blank value?
ASKER CERTIFIED SOLUTION
Avatar of victorli
victorli
Flag of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial