943,903 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 768
  • Java RSS
Aug 21st, 2009
0

Need To Solve The Equation

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moyphus is offline Offline
11 posts
since May 2009
Aug 21st, 2009
0

Re: Need To Solve The Equation

Click to Expand / Collapse  Quote originally posted by moyphus ...
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.
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Aug 21st, 2009
0

Re: Need To Solve The Equation

Click to Expand / Collapse  Quote originally posted by puneetkay ...
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,.,.,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moyphus is offline Offline
11 posts
since May 2009
Aug 21st, 2009
0

Re: Need To Solve The Equation

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,.,.,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moyphus is offline Offline
11 posts
since May 2009
Aug 21st, 2009
0

Re: Need To Solve The Equation

Click to Expand / Collapse  Quote originally posted by moyphus ...
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.
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Aug 21st, 2009
0

Re: Need To Solve The Equation

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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moyphus is offline Offline
11 posts
since May 2009
Aug 21st, 2009
0

Re: Need To Solve The Equation

Is formula a String? :
Java Syntax (Toggle Plain Text)
  1. formula = "((10000.00+4000.00)*(30/100))";

If yes then this will not work:
Java Syntax (Toggle Plain Text)
  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 (+, -, *, /)
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC