2

This is a toString method for formatting the terms of a polynomial and adding them to a String. Its final output is something like "+ 2x^2 + 2x + 1" How would I go about removing the first plus sign? Thanks

//toString method for returning ReesePolynomials in polynomaial format(for ReeseClient)
       public String toString()
       {
          String output = "";
          //the following are situations for formatting the output of each term and adding them to String output
          for (int i = 0; i < TermLength; i++)  // For the number of terms stated by the user earlier, values will be given for each ReeseTerm in the poly array
             {  
                if (poly[i].getExpo() == 0 && poly[i].getCoeff() > 0)       
                {
                   output += " + " + poly[i].getCoeff();
                }

                else if (poly[i].getExpo() == 0 && poly[i].getCoeff() < 0)
                {
                   output += " " + poly[i].getCoeff();
                }

                else if (poly[i].getCoeff() == 1 && (poly[i].getExpo() != 0 && poly[i].getExpo() != 1))
                {
                   output += " + x^" + poly[i].getExpo();   
                }

                else if (poly[i].getCoeff() == 1 && (poly[i].getExpo() == 1))
                {
                   output += " + x";    
                }

                else if (poly[i].getExpo() == 1 && poly[i].getCoeff() > 0)
                {
                   output += " + " + poly[i].getCoeff() + "x";
                }

                else if (poly[i].getExpo() == 1 && poly[i].getCoeff() < 0)
                {
                   output += " " + poly[i].getCoeff() + "x";
                }

                else if (poly[i].getCoeff() < 0 && (poly[i].getExpo() > 1 || poly[i].getExpo() < -1))
                {
                   output += " " + poly[i].getCoeff() + "x^" + poly[i].getExpo();
                }

                else if (poly[i].getCoeff() == 0)
                {}

                else
                {
                   output += " + " + poly[i].getCoeff() + "x^" + poly[i].getExpo();
                }

             }

       return output;       // the final string output is returned to be printed when called upon in main
       }
Void Star
  • 2,401
  • 4
  • 32
  • 57
user3019579
  • 71
  • 10
  • Don't put it there in the first place. – user207421 Nov 26 '13 at 22:40
  • That's much more difficult than fixing the fencepost problem after the fact, except in rare cases (such as when recursively going through a list and printing it to the console or some such). – Void Star Nov 27 '13 at 01:47
  • @BigEndian I do not agree. Not making a mistake is much simpler than fixing it after you've made it. – user207421 Nov 27 '13 at 09:13

2 Answers2

3

Right before your return statement, use the substring method:

output = output.substring(2);

This gives you all characters from index 2 to end. http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

The problem you are facing is commonly called the fencepost problem, or off-by-one error. http://en.wikipedia.org/wiki/Off-by-one_error

Void Star
  • 2,401
  • 4
  • 32
  • 57
1

Try
output = output.subString(2);
return output;

Isaac
  • 625
  • 1
  • 12
  • 30