user1276066
user1276066

Reputation: 13

Extracting polynomial variables from String input via a constructor

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 3x^y 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...

this:

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

is set to:

trinomial[0] = new Term ("3x^y");

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.

public class TrinomialTest {
    static void printTrinomial(Term[] x) {
        for (int i = 0; i < x.length; i++) {
            //AUTOMATICALLY CALLS TOSTRING() WHEN PRINTLN
            System.out.print(x[i]);
        }
    }

    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 && c > 0)
            return ("" + "+" + c + variable);
        else if (exponent == 1 && c < 0)
            return ("" + c + variable);
        else if (exponent == 0 && c > 0)
            return ("" + "+" + c);
        else if (exponent == 0 && c < 0)
            return ("" + c);

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

Upvotes: 1

Views: 1943

Answers (1)

blackcompe
blackcompe

Reputation: 3190

Simple solution:

String term = "32x^33";
String[] tokens = term.split("\\^");
String coeff = tokens[0].substring(0, tokens[0].length()-1);
String var = tokens[0].substring(tokens[0].length()-1, tokens[0].length());
String exp = tokens[1];
System.out.println("coeff = "+coeff+", var = "+var+", exp = "+exp);

Upvotes: 2

Related Questions