?
Solved

Substraction on 2 int type number

Posted on 2003-03-31
7
Medium Priority
?
205 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:fks2
7 Comments
 
LVL 3

Expert Comment

by:paskal
ID: 8243926
You're using the variable 'pl' in your substratcion instead of 'plint'. I guess that should make the difference.
0
 
LVL 4

Expert Comment

by:thanassis
ID: 8243960
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;
}
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 8244049



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

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

int result=ai=bi;



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 19

Expert Comment

by:cheekycj
ID: 8245860
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
0
 

Author Comment

by:fks2
ID: 8250291
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?
0
 

Author Comment

by:fks2
ID: 8250399
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?
0
 
LVL 1

Accepted Solution

by:
victorli earned 200 total points
ID: 8250679
The reason of the error is:
"ori_numbe" and "latest_number" is declared as "int" type and you can not compare them with the empty string "" which has got "String" type.

> guys, I think one of my "Int" type value is blank.
This is wrong! Actually one of your "daycount.getPo_number()" or "daycount.getLC_number()" is blank.

Try this:

...
int result=0;
String po = daycount.getPo_number();
String lc = daycount.getLC_number();

if (po != null && !po.equals("") && lc != null && !lcpo.equals("")) {
      try {
         int latest_number = Integer.parseInt(po);          
         int ori_number = Integer.parseInt(lc);
         result=ori_number - latest_number;
         out.println("Result :" + result);
      } catch (NumberFormatException ne) {  
         ne.printStackTrace();
      }
else {
  out.println("Either one variable is blank");
}
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Day by day nothing changes but when u look back, everything is different". That quote precisely describes today’s digital era. For example, you may not have noticed the change, but Voice Search is now all around us.
A quick solution showing how to control and open a POS Cash Register Drawer using VBA with MS Access.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question