Need To Solve The Equation

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 11
Reputation: moyphus is an unknown quantity at this point 
Solved Threads: 0
moyphus moyphus is offline Offline
Newbie Poster

Need To Solve The Equation

 
0
  #1
Aug 21st, 2009
Hi To All,

I have an equation in string like this

((BASIC+HRA)*(30/100))
Using replace function
Now i replaced this into values...
String formula = ((10000+3000)*(30/100))

whole thing as a string now 'Formula'...

How can i calculate this formula

float amt = formula;

Now i have an error...type casting....i can't calculate this amt
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Need To Solve The Equation

 
0
  #2
Aug 21st, 2009
Originally Posted by moyphus View Post
Hi To All,

I have an equation in string like this

((BASIC+HRA)*(30/100))
Using replace function
Now i replaced this into values...
String formula = ((10000+3000)*(30/100))

whole thing as a string now 'Formula'...

How can i calculate this formula

float amt = formula;

Now i have an error...type casting....i can't calculate this amt

float amt = Float.parseFloat(formula);

and btw why are you storing equation result in string ?

better way :

float amt = ((10000+3000)*(30/100));

Regard,
Last edited by puneetkay; Aug 21st, 2009 at 9:00 am.
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: moyphus is an unknown quantity at this point 
Solved Threads: 0
moyphus moyphus is offline Offline
Newbie Poster

Re: Need To Solve The Equation

 
0
  #3
Aug 21st, 2009
Originally Posted by puneetkay View Post
float amt = Float.parseFloat(formula);

and btw why are you storing equation result in string ?

better way :

float amt = ((10000+3000)*(30/100));

Regard,
Hi Thanks For The Reply...

i already used that,But its not worked.....If There any other solution to solve this,.,.,
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: moyphus is an unknown quantity at this point 
Solved Threads: 0
moyphus moyphus is offline Offline
Newbie Poster

Re: Need To Solve The Equation

 
0
  #4
Aug 21st, 2009
Hi,.,

Actually Before that i have like this ,.,((BASIC+HRA)*(30/100))
This is coming from database as a whole equation...
I'm replacing the Strings into values based on that BASIC And HRA,,,.

So it is String type,...i need to calculate that based on that formula,.,.,
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Need To Solve The Equation

 
0
  #5
Aug 21st, 2009
Originally Posted by moyphus View Post
Hi Thanks For The Reply...

i already used that,But its not worked.....If There any other solution to solve this,.,.,

double amt = ((10000.0+3000.0)*(30.0/100.0));
System.out.println(amt);

Working fine.

Output : 3900.0

if still not working, Please post the code related to the problem.
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: moyphus is an unknown quantity at this point 
Solved Threads: 0
moyphus moyphus is offline Offline
Newbie Poster

Re: Need To Solve The Equation

 
0
  #6
Aug 21st, 2009
Hi,,Tanx,.,

Here my code,

formula = compList.getPrformula(); // Here i'm getting value Like ((BASIC+HRA)*(30/100))
derFrom = compList.getDerivedFrom(); //Here, BASIC,HRA
derFrom1 = derFrom.split(",");
for (int i=0;i<derFrom1.length;i++){
CharSequence ch = "+";
CharSequence ch1 = "-";
CharSequence ch2 = "*";
CharSequence ch3 = "/";
String[] drFrom=null;
if (derFrom1[i].contains(ch))
drFrom = derFrom1[i].split("\\+");
if (derFrom1[i].contains(ch1))
drFrom = derFrom1[i].split("\\-");
if (derFrom1[i].contains(ch2))
drFrom = derFrom1[i].split("\\*");
if (derFrom1[i].contains(ch3))
drFrom = derFrom1[i].split("\\/");
if (!("".equals(drFrom) || drFrom == null)){
if (drFrom.length>0){
comp = new ArrayList<String>();
comp1 = new ArrayList<String>();
String[] com={};
for (int j = 0;j<this.availPayroll.size();j++){ comp.add(String.valueOf(this.availPayroll.get(j).getAmountStart()));
//Here In comp, i have 10000.00
}
for (int k=0;k<drFrom.length;k++){
for (int j=0;j<this.availPayroll.size();j++){
if (drFrom[k].equalsIgnoreCase(String.valueOf(this.availPayroll.get(j).getPrcompId())))
formula = formula.replaceAll(drFrom[k], this.comp.get(j));
here i'm replacing 10000.00 instead of BASIC
4000.00 instead of HRA
}
}
}
}
else {
comp = new ArrayList<String>();
for (int j = 0;j<this.availPayroll.size();j++){
comp.add(String.valueOf(this.availPayroll.get(j).getAmountStart()));
}
for (int j=0;j<this.availPayroll.size();j++){
if (derFrom1[i].equalsIgnoreCase(String.valueOf(this.availPayroll.get(j).getPrcompId())))
formula = formula.replaceAll(derFrom1[i], this.comp.get(j));
here i'm replacing 10000.00 instead of BASIC
4000.00 instead of HRA
}
}

// Now ,formula = ((10000.00+4000.00)*(30/100))

double amout = Double.valueOf(formula);

It Produces an error like "For Input String"
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Need To Solve The Equation

 
0
  #7
Aug 21st, 2009
Is formula a String? :
  1. formula = "((10000.00+4000.00)*(30/100))";

If yes then this will not work:
  1. double amout = Double.valueOf(formula);

The String formula has to have a numeric value. Like "2" or "-100.345".
If it has value: "((10000.00+4000.00)*(30/100))" it will not work.
You need to extract the numbers from the String formula, turn them into numbers (parseDouble() or parseFloat()) and then apply the operators (+, -, *, /)
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC