Java - Extracting polynomial variables from String input via a constructor
<p>I was hoping somebody could help me with with this. I am trying to add a constructor to allow the input of polynomial data from a string in the form <code>3x^y</code> and put the coefficient, variable, and exponent values into the corresponding variables defined in a class. The constructor I have implemented allows the program to compile, but will freeze when running the application if...</p>

<p>this:</p>

<pre><code>trinomial[0] = new Term (3,'x',2);
</code></pre>

<p>is set to:</p>

<pre><code>trinomial[0] = new Term ("3x^y");
</code></pre>

<p>Basically, I'm trying to get the defined class to accept both forms of input (string and double/char/exponent formats). Any help with this would be greatly appreciated.</p>

<pre><code>public class TrinomialTest {

  static void printTrinomial(Term [] x) {

    for(int i=0;i&lt;x.length;i++){
       System.out.print(x[i]);   //AUTOMATICALLY CALLS TOSTRING() WHEN PRINTLN

    }
  }

public static void main(String[] args) {

Term [] trinomial = new Term [3];

trinomial[0] = new Term (3,'x',2);

trinomial[1] = new Term (12, 'x', 1);

trinomial[2] = new Term (-23, 'x', 0);

printTrinomial(trinomial);

}

}

class Term {

private double coefficient; //the number

private char variable; // the letter

private int exponent;



public Term (double c, char v, int e) {

coefficient = c;

variable = v;

exponent = e;

}
//New Constructor
//
public Term (String Term) {

String c = "";
String v = "";
String e = "";
int exponentIndex = 0; 
exponentIndex = Term.indexOf("^");


if(Term.substring(0,1).matches("[0-9]")){
    c = Term.substring(0,1);
    v = Term.substring(1,2);
}   
else{
    c = "0";
    v = Term.substring(0,1);
}


do{
    e = Term.substring(exponentIndex,exponentIndex+1);
}while(Term.indexOf("^")!=-1);



coefficient = Double.parseDouble(c);
variable = v.charAt(0);
exponent = Integer.parseInt(e);
}

public String toString() {

  int c = (int)coefficient;



if (exponent==1&amp;&amp;c&gt;0)
return (""+"+"+c+variable);
else if (exponent==1&amp;&amp;c&lt;0)
return (""+c+variable); 
else if (exponent==0&amp;&amp;c&gt;0)
return (""+"+"+c);
else if (exponent==0&amp;&amp;c&lt;0)
return (""+c);


return (""+c+variable+"^"+exponent);    
}




}           
</code></pre>
