Input pattern (first case): 4x²-8x+6 (or 4x^2-8x+6)
Input pattern (2nd case): 3x-8y=2
First Case: For example- If the user inputs a string such as 4x²-8x+6 (or 4x^2-8x+6) The output should contain all the positive and negative integers extracted from the string and should get stored in an array so that it can be used further in the program i.e. output should be
4
2 //(² or ^2 to check whether it is a quadratic equation, if it's not 2 and is 3 or //something else,the program should terminate here.)
-8
6
2nd Case(another program): If the user inputs a string such as 3x-8y=2. The output should be
3
-8
2
And stored in an array so that they can be accessed further in the program.
Any help would be greatly beneficial for me.
import java.util.*;
public class ss
{
public void dd()
{
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
double a=sc.nextDouble(); //Accepts numerical coefficient of x² in the
//quadratic form of ax²+bx+c
double b=sc.nextDouble(); //Accepts numerical coefficient of x in the
//quadratic form of ax²+bx+c
double c=sc.nextDouble(); //Accepts constant c in the
//quadratic form of ax²+bx+c
double discriminant=(Math.pow(b,2))-(4*a*c);
double firstroot= -b+Math.sqrt(discriminant)/(2*a);
double secondroot= -b-Math.sqrt(discriminant)/(2*a);
System.out.println("Roots Of The Equation Are: "+first root+" and "+secondroot);
}
}
I want the above code to work without taking each value of the coefficients and constants from the user by extracting all the coefficients either positive or negative from a single string input from the user in form of a quadratic equation.