Problem: Java program 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