samedi 18 avril 2015

java program to extract cofficents from quadratic equation

Problem : Java pgm to split the coefficients from a quadratic equation eg if Input string is


String str1; str1 = "4X2-4X-42=0 "


so I need to split the coefficients from the given input string and to get output as a=4 b=-4 c = -42


i tried this



String equation = "ax2+bx-c=0";
String[] parts = equation.split("\\+|-|=");
for (int i=0; i<parts.length-2; i++) {
String part = parts[i].toLowerCase();
System.out.println(part.substring(0, part.indexOf("x")));
}
System.out.println(parts[2]);


but i got the output as 23x2 and 4x and 4 Actual output needed is 23 ,- 4 , 4


Aucun commentaire:

Enregistrer un commentaire