I am trying to split a string of a polynomial containing a couple options:
1. "-x"
2. "x"
3. "-7","-70","-700" etc . . .
4. "-7","-70","-700" etc . . .
5. "-7x","-70x","-700x" etc . . .
6. "7x","70x","700x" etc . . .
7. "x^2","x^20","x^200" etc . . .
8. "-x^2","-x^20","-x^200" etc . . .
9. "-5x^5","-50x^50","-500x^500" etc . . .
10. "5x^5","50x^50","500x^500" etc . . .
I have successfully used a str.split() to split the polynomials in individual terms as shown above. However I want to then seperate the coefficient and exponent. I wouldn't post this on here except that I have read through the java regex and am not understanding.
I read through this: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
I want to use a enhanced for loop as such with the string I want to omit in each loop:
for (String term : polynomialTerm.split("x") {
// for terms like these -5x, -50x, -500x && 5x, 50x, 500x
// term will be the coefficient
}
for (String term : polynomialTerm.split("x^") {
// for terms like these x^2, x^20, x^200 && -2x^2, -20x^20, -200x^200 && 2x^2, 20x^20, 200x^200
// term could be the coefficient or exponent
}
for (String term : polynomialTerm.split("-x^") {
// for terms like these -x^2, -x^20, -x^200
// term should then be the exponent
}
I then will use a parseInt() to get the coefficient or exponent from the loop with if and else if statements to ensure I grab the right information