| | |
Need To Solve The Equation
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2009
Posts: 11
Reputation:
Solved Threads: 0
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
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
•
•
•
•
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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: May 2009
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
float amt = Float.parseFloat(formula);
and btw why are you storing equation result in string ?
better way :
float amt = ((10000+3000)*(30/100));
Regard,
i already used that,But its not worked.....If There any other solution to solve this,.,.,
•
•
•
•
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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: May 2009
Posts: 11
Reputation:
Solved Threads: 0
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"
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"
Is formula a String? :
If yes then this will not work:
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 (+, -, *, /)
Java Syntax (Toggle Plain Text)
formula = "((10000.00+4000.00)*(30/100))";
If yes then this will not work:
Java Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Prandtl equation solver (Python)
- Physics Game (Posting Games)
- From phrase image (Python)
- Tkinter Spreadsheet (Python)
- How to carry out partial fraction decomposition (C)
- Excel solver using VB6 (Visual Basic 4 / 5 / 6)
- hi i have a question!!!!!! (^o^) (C++)
- Why won't this compile (C++)
Other Threads in the Java Forum
| Thread Tools | Search this Thread |
6 actuate android api applet application array arrays automation balls binary bluetooth bold business c++ chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging ebook eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui hql html ide image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie nextline numbers parameter php print problem program programming project recursion recursive scanner screen sell server set size sms sort sql string sun swing swt threads time tree user websites windows






