How do I print out a polynomial in a separate method?

Viewed 19

I'm trying to print a polynomial expression into a separate text file. For example, 3x^2 + 2x + 1. The coefficients (3,2,1) are variables that were scanned. I have tried doing ps.printf("a + "x^2" + b + "x" + c); but it's not working. I also tried using %d and %f to substitute for a,b, and c, but nothing seems to be working. I need the polynomial to be printed in a separate method. Here is my code so far:

package polynomials;
import java.util.Scanner;
import java.io.PrintStream;
public class Polynomials {
    public static int a, b, c;
    public static double exponent;

    public static PrintStream ps;

    public static void main(String[] args) throws Exception {


        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter three values for a, b, and c.");
        System.out.println("Enter three zeros to stop.");

        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();

        ps = new PrintStream("results.txt");

        ps.printf("a = " + a + "\t" + "b = " + b + "\t" + "c = " + c);
        findPolynomial();

    }

    public static void printPolynomial() throws Exception {
        System.out.printf(%d + 'x^2' + %d + 'x' + %d, a, b, c);

    }

}
0 Answers
Related