I'm not sure if it's the way I'm sorting this ArrayList or the way I'm constructing new Term objects. Can anyone tell me what's wrong? My program is too long, so I'll narrow it down to the parts that are giving me trouble:

The declaring of the terms and new polynomial object in the main method:

ArrayList<Term> myTermList = new ArrayList<Term>();
myTermList.add(new Term("+5x"));
myTermList.add(new Term("-4x^2"));
myTermList.add(new Term("+9"));
Polynomial p2 = new Polynomial(myTermList);
System.out.println("Polynomial 2 toString Call: " + p2.toString());

The Term class:

class Term {
	private double coeff = 0;
	private char var = 'x';
	private int expo = 0;

	/*
	 * These booleans are used to keep track of whether or not a term has a coefficient,
	 * a variable, and an exponent, as well as whether or not it is negative.
	 */
	private boolean hasCoeff = false;
	private boolean hasVar = false;
	private boolean hasExpo = false;

	private boolean isNegative = false;

	private String permaTerm = ""; // Used to store a constant copy of the term passed in the constructor.

	public Term(String fullTerm) {
		String termString = fullTerm;
		permaTerm = fullTerm;
		char temp;

		/*
		 * Steps through the term, character-by-character, removing the + or - sign if one exists,
		 * determining if the term has a variable, and determining if the term has an exponent.
		 */
		for(int i = 0;i < termString.length();i++) {
			temp = termString.charAt(i);

			if(temp == '^') {
				hasExpo = true;
			}
			if((temp == '+')||(temp == '-')) {
				if(temp == '-') {
					isNegative = true;
					termString = remCharAt(termString, i); // Remove the - sign.
					i = 0;
				} else {
					isNegative = false;
					termString = remCharAt(termString, i); // Remove the + sign.
					i = 0;
				}
			}
			if(Character.isLetter(temp)) {
				var = temp;
				hasVar = true;
				termString = remCharAt(termString, i); // Remove the variable.
				i = 0;
			}
		}

		// termString now contains "coeff^expo" assuming both exist.

		String delims = "[\\^ ]+";
		String splitTerm [] = termString.split(delims); // Splits termString using spaces and the ^ character as delimiters.

		if((splitTerm.length > 0)&&(!hasExpo)) { // If there is at least one number left, and there is no exponent.
			hasCoeff = true;
			coeff = Double.parseDouble(splitTerm[0]);
			if(hasVar) { // If there was a variable in the string.
				expo = 1;
			} else {
				expo = 0;
			}
		} else if((splitTerm.length > 0)&&(hasExpo)) { // If there is at least one number left, and there is an exponent.
			if(splitTerm.length > 1) { // If there are at least two numbers left.
				hasCoeff = true;
				coeff = Double.parseDouble(splitTerm[0]);
				expo = Integer.parseInt(splitTerm[1]);
			} else {
				coeff = 1;
				expo = Integer.parseInt(splitTerm[0]);
			}
		}

		System.out.println("Term: " + fullTerm); // Shows the term that was passed to the constructor.
		if(hasCoeff) { // If there is a coefficient in the term.
			System.out.printf("Coefficient: ");
			if(isNegative) {
				System.out.printf("-");
			}
			System.out.printf("%.2f", coeff);
			System.out.println();
		}
		if(hasVar) { // If there is a variable in the term.
			System.out.println("Variable: " + var);
		}
		if(hasExpo) { // If there is an exponent in the term.
			System.out.println("Exponent: " + expo);
		}
	}

	/*
	 * Method - remCharAt()
	 * Function - Removes character at specified position in a string.
	 */
	private String remCharAt(String fullTerm, int pos) {
		StringBuffer buf = new StringBuffer(fullTerm.length() - 1);
		buf.append( fullTerm.substring(0,pos) ).append( fullTerm.substring(pos+1) );
		return buf.toString();
	}

	/*
	 * Constructs a string with the term in it and returns it.
	 */
	public String getTerm() {
		String result = "";

		if(isNegative) {
			result += "- ";
		} else {
			result += "+ ";
		}

		if(hasCoeff) {
			result += coeff;
		}

		if(hasVar) {
			result += var;
		}

		if(hasExpo) {
			result += "^" + expo;
		}

		return result;
	}

	public boolean hasCoefficient() {
		return hasCoeff;
	}
	public boolean hasVariable() {
		return hasVar;
	}
	public boolean hasExponent() {
		return hasExpo;
	}

	public double getCoeff() {
		return coeff;
	}
	public char getVar() {
		return var;
	}
	public int getExpo() {
		return expo;
	}

	public void setCoeff(double newCoeff) {
		if(newCoeff < 0) {
			isNegative = true;
		} else {
			isNegative = false;
		}

		coeff = newCoeff;

		if(coeff != 0) {
			hasCoeff = true;
			hasVar = true;
		} else {
			hasCoeff = false;
			hasVar = false;
		}
	}
	public void setVar(char newVar) {
		var = newVar;
	}
	public void setExpo(int newExpo) {
		expo = newExpo;

		if(expo > 1) {
			hasExpo = true;
		} else {
			hasExpo = false;
		}
	}
}

The Polynomial class' second constructor that copies the contents of the parameter ArrayList to the myTerms ArrayList:

public Polynomial(ArrayList<Term> terms_list) {
    // Copy the terms from the list passed into the constructor into the myTerms list.
    myTerms = terms_list;
    permaPoly = toString();
    sortTerms();
}

The sortTerms() method in the Polynomial class:

private void sortTerms() {
    for(int i = 0;i < (myTerms.size() - 1);i++) {
        for(int j = i + 1;j < myTerms.size();j++) {
            if((myTerms.get(j).getExpo()) > (myTerms.get(i).getExpo())) {
                Term temp = new Term(myTerms.get(j).getTerm());
                myTerms.set(j, myTerms.get(i));
                myTerms.set(i, temp);
            }
        }
    }
}

Anyone know what I'm doing wrong? Here's the error I got:

Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
at java.lang.Double.parseDouble(Double.java:510)
at Term.<init>(CIS227_Assignment_07.java:77)
at Polynomial.sortTerms(CIS227_Assignment_07.java:251)
at Polynomial.<init>(CIS227_Assignment_07.java:244)
at CIS227_Assignment_07.main(CIS227_Assignment_07.java:337)

I've seen some suggestions on Google saying I should use the Collections.sort() method, but I'd really like to write my own. Does anyone know what's wrong with my sort method?

Ah, I think I got it. The problem was the "getTerm" method. It must not have been returning a valid Term. I changed it to return the permaTerm variable, as that's what I should have been doing in the first place, rather than re-constructing the Term.

This article has been dead for over six months. Start a new discussion instead.