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.NumberFormatExce ption:
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
java.lang.NumberFormatExce
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
You're using the variable 'pl' in your substratcion instead of 'plint'. I guess that should make the difference.
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 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
it should be
int result = ai - bi;
try this:
<%
int result=0;
try {
int ab = Integer.parseInt(javabean.
int pl = Integer.parseInt(javabean.
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
ASKER
CJ,
java.lang.NumberFormatExce ption
my code is something like this,
int result=0;
try {
String ab = javaBean.getAB();
int latest_number = Integer.parseInt(daycount. getPo_numb er());
int ori_number = Integer.parseInt(daycount. getLC_numb er());
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?
java.lang.NumberFormatExce
my code is something like this,
int result=0;
try {
String ab = javaBean.getAB();
int latest_number = Integer.parseInt(daycount.
int ori_number = Integer.parseInt(daycount.
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?
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.