title: Single Variable Polynomial (from text file)
body: <p>I want to modify the below code to read the nodes from a text file (vs. hard-coded values)</p>
  
  <p>Furthermore, read data from text file in the format:</p>
  
  <p>P1 = 3 5 1 -1 0 8</p>
  
  <p>P2 = 5 6 2 -1 1 7 0 -4</p>
  
  <p>etc... </p>
  
  <p>Name the values P(x) and input the remaining data. Any advice?</p>
  
  <pre><code>import java.io.DataInputStream;
  import java.util.LinkedList;
  import java.util.ListIterator;
  
  // A term contains the coefficient and the exponent of x.
  class Term {
  public Term(int c, int d) {
      coeff = c;
      degree = d;
  }
  
  public Term multiply(Term other) {
      return new Term(coeff * other.coeff, degree + other.degree);
  }
  
  public void add(int c) {
      coeff += c;
  }
  
  public void sub(int c) {
      coeff -= c;
  }
  
  public int getCoeff() {
      return coeff;
  }
  
  public int getDegree() {
      return degree;
  }
  
  private int coeff;
  private int degree;
  }
  
  // A polynomial made up of terms.
  class Polynomial {
  // Construct a Polynomial object.
  public Polynomial() {
      terms = new LinkedList();
  }
  
  public Polynomial add(Polynomial p) {
      Polynomial r = new Polynomial();
      ListIterator iterator = terms.listIterator();
      while (iterator.hasNext()) {
          r.add((Term) iterator.next());
      }
      ListIterator pIterator = p.terms.listIterator();
      while (pIterator.hasNext()) {
          r.add((Term) pIterator.next());
      }
      return r;
  }
  
  public Polynomial sub(Polynomial p) {
      Polynomial r = new Polynomial();
      ListIterator iterator = terms.listIterator();
      while (iterator.hasNext()) {
          r.sub((Term) iterator.next());
      }
      ListIterator pIterator = p.terms.listIterator();
      while (pIterator.hasNext()) {
          r.sub((Term) pIterator.next());
      }
      return r;
  }
  
  public Polynomial multiply(Polynomial p) {
      Polynomial r = new Polynomial();
      ListIterator iterator = terms.listIterator();
      while (iterator.hasNext()) {
          ListIterator pIterator = p.terms.listIterator();
          Term t1 = (Term) iterator.next();
          while (pIterator.hasNext()) {
              Term t2 = (Term) pIterator.next();
              r.add(t1.multiply(t2));
          }
      }
      return r;
  }
  
  // Adds a coefficient and degree as a new Term.
  
  public void addTerm(int c, int d) {
      add(new Term(c, d));
  }
  
  // Adds a term
  
  public void add(Term t) {
      int c = t.getCoeff();
      int d = t.getDegree();
  
      ListIterator iterator = terms.listIterator();
      while (iterator.hasNext()) {
          Term current = (Term) iterator.next();
          if (d == current.getDegree()) {
              if (c == -current.getCoeff())
                  iterator.remove();
              else
                  current.add(c);
              return;
          } else if (d &lt; current.getDegree()) {
              iterator.previous();
              iterator.add(t);
              return;
          }
      }
      iterator.add(t);
  }
  
  public void sub(Term t) {
      int c = t.getCoeff();
      int d = t.getDegree();
  
      ListIterator iterator = terms.listIterator();
      while (iterator.hasNext()) {
          Term current = (Term) iterator.next();
          if (d == current.getDegree()) {
              if (c == current.getCoeff())
                  iterator.remove();
              else
                  current.add(-c);
              return;
          } else if (d &lt; current.getDegree()) {
              iterator.previous();
               iterator.add(t);
              current.add(-c);
              return;
          }
      }
      iterator.add(t);
  
  }
  
  // Prints the polynomial.
  public void print() {
      ListIterator iterator = terms.listIterator();
  
      while (iterator.hasNext()) {
          Term current = (Term) iterator.next();
          if (current.getCoeff() != 0) {
              if ((current.getCoeff() &lt; 0) || (!(current.getDegree() != 0))) {
                  System.out.print(" ");
              }
  
              else {
                  System.out.print(" + ");
              }
              System.out.print(current.getCoeff());
  
              if (current.getDegree() &gt; 0) {
                  System.out.print("x");
                  if (current.getDegree() &gt; 1)
                      System.out.print("^" + current.getDegree());
              }
          }
      }
      System.out.println();
  }
  
  @SuppressWarnings("deprecation")
  public char getChar() {
      char val = ' ';
      String buffer = "";
  
      DataInputStream ds = new DataInputStream(System.in);
      try {
          buffer = ds.readLine();
          val = buffer.charAt(0);
      } catch (Exception e) {
      }
  
      return (val);
  }
  
  public char enterNext() {
      System.out.println("Enter next Polynomial? ");
      char ans = this.getChar();
      switch (ans) {
      case 'n':
      case 'N':
          System.out.println("All Done.");
          System.exit(0);
          break;
      case 'y':
      case 'Y':
          System.out.println("Enter next one, please!");
      }
      return ans;
  }
  
  private LinkedList terms;
  }
  
  // A test program for Polynomial and Term classes.
  
  public class RPC {
  private static char ans = 'y';
  
  public static void main(String[] args) {
      Polynomial p = new Polynomial();
      do
       {
      p.addTerm(10, 0);
      p.addTerm(-1, 1);
      p.addTerm(9, 7);
      p.addTerm(5, 10);
      System.out.print("f(x) = ");
      p.print();
      System.out.println();
      Polynomial q = new Polynomial();
       do
      {
      q.addTerm(1, 0);
      q.addTerm(-11, 1);
      q.addTerm(19, 7);
      q.addTerm(15, 10);
      System.out.print("q(x) = ");
      q.print();
       p.enterNext();
      }
       while (!(ans != 'y') | !(ans != 'Y'));
      System.out.println();
  
      Polynomial a = p.add(q);
      System.out.print("f(x) + q(x) = ");
      a.print();
      Polynomial s = p.sub(p);
      s.print();
      Polynomial m = p.multiply(q);
      System.out.print("f(x) * q(x) = ");
      m.print();
      System.exit(0);
  
  } while (ans != 1000);
  }
  }
  </code></pre>