Extracting coefficants and variable portion of string (java)

83 views Asked by At

I'm writing a basic polynomial solver, and so far, I've tokenized my input as follows.

Example: x^2+3.5x+4x+3=0 becomes [x^2,7.5x,3] (call this terms1). I generate this array by separating on "+", and that works well.

I want to extract the coefficients and variables from each of these so it would transform into two arrays like the following: [x^2,x,""] [1,7,3]

So far my code looks like this (but it accidentally matches the exponent and doesn't match the x squared which as an implicit coefficient)

for(String term : terms1){
        Scanner s = new Scanner(term);
        coff = s.findInLine("\\d+(\\.\\d+)?" );
        var = s.next();
        System.out.println(coff);
        System.out.println(var);
}
0

There are 0 answers