0

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

joshuatvernon
  • 1,530
  • 2
  • 23
  • 45
  • Not sure if `String#split` is the right option here. I'd advise you edit your question, and add the source text as literal (i.e. a sufficient portion of it to cover all your test cases), then your desired outcome per scenario and what you have concretely tried to reach it. – Mena Jan 19 '16 at 10:11
  • @BorisPavlović good find. – Mena Jan 19 '16 at 10:16
  • @BorisPavlović I am not asking how to split the polynomial e.g. (-5x^5+4x^4+3x^3) but rather the resulting monomial that I get after. As in I want to split "-5x^5" so that I can parse the coefficient and exponent to to variables and don't understand the regex to split the -5 from the 5 to delimit "x^" and more so "-x^". I'm currently trying "x\\^" && "-x\\^" – joshuatvernon Jan 19 '16 at 10:21

0 Answers0