Nicholas
Nicholas

Reputation: 679

How to detect polynomials in Java

Suppose I have a polynomial -x^2 + 2x - 1 = 0. It is read from a file.

I have the code that analyzes each character of the polynomial.

I want to create an extra step that compacts the polynomial(so the white spaces gets eliminated) so I can check if the string is in fact a polynomial which I can easily do by just checking the last 2 index of the polynomial which is the equal sign and the zero like this: (=0)

Problem is some polynomial length have different lengths which gave me the thought to use an ArrayList. Problem is I cannot declare my ArrayList to be of type Character to store each character in the sequential index of an ArrayList.

public void createEquationNoWhiteSpaces(){
    // it cannot be done because there is no ArrayList of characters
    textArrayList = new ArrayList<String>();
    for(int i = 0; i < text.length(); i++){
        // Store the characters of the polynomial in an ArrayList
        // because each polynomial has different length
        if(text.charAt(i) != ' ')
            textArrayList = text.charAt(i);
    }
}

Upvotes: 0

Views: 553

Answers (2)

DominikM
DominikM

Reputation: 1052

st.replaceAll("\\s","") 

removes all whitespace in string st

Upvotes: 1

jacobm
jacobm

Reputation: 14035

If you want to use an array, you can certainly declare an ArrayList<Character>. However, you might want to use a StringBuilder instead of a list for this purpose anyway.

Upvotes: 1

Related Questions