?
Solved

Substraction on 2 int type number

Posted on 2003-03-31
7
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

765 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