Bottom line, I have to input two files (with polynomials), ensure they are sorted correctly, and merge them into one sorted output file. The code below compiles and runs smooth; however, no matter what I input, the files come back unsorted. All of the input files were provided and are supposed to work (except one, but that's not the problem). Here's my code:

andCode:`import java.io.BufferedReader;`

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class Project1 {

public static void main(final String[] args) throws FileUnsorted, IOException {

Scanner scanner = new Scanner(System.in);

String inputFile1;

String inputFile2;

System.out.println("Enter first file name with extension: ");

inputFile1 = scanner.nextLine();

System.out.println("Enter second file name with extension: ");

inputFile2 = scanner.nextLine();

try {

BufferedReader file1 = new BufferedReader(new FileReader(inputFile1));

BufferedReader file2 = new BufferedReader(new FileReader(inputFile2));

checkSorted(file1);

checkSorted(file2);

}

catch (FileUnsorted ex) {

System.exit(1);

}

}

private static BufferedReader checkSorted(BufferedReader fileName) throws FileUnsorted, IOException {

Polynomial p1 = null;

Polynomial p2 = null;

String line;

while ((line = fileName.readLine()) != null) {

p2 = new Polynomial(line);

if (p1 != null) {

if (p1.compareTo(p2) > 0) {

throw new FileUnsorted();

}

}

p1 = p2;

}

return new BufferedReader(fileName);

}

}

andCode:`import java.io.BufferedReader;`

import java.io.IOException;

import java.util.Scanner;

public class Polynomial implements Comparable<Polynomial> {

int[] coef;

int deg;

public Polynomial input(BufferedReader fileName) throws IOException {

return new Polynomial(fileName.readLine());

}

public Polynomial(String polynomial) {

Scanner line = new Scanner(polynomial);

int coefficient = line.nextInt();

int deg = line.nextInt();

coef = new int[deg + 1];

coef[deg] = coefficient;

deg = degree();

while (line.hasNextInt()) {

coefficient = line.nextInt();

deg = line.nextInt();

coef[deg] = coefficient;

}

}

public int degree() {

int d = 0;

for (int i = 0; i < coef.length; i++) {

if (coef[i] != 0)

d = i;

}

return d;

}

public String toString() {

if (deg == 0)

return "" + coef[0];

else if (deg == 1)

return coef[1] + "x + " + coef[0];

String s = coef[deg] + "x^" + deg;

for (int i = deg - 1; i >= 0; i--) {

if (coef[i] == 0) continue;

else if (coef[i] > 0)

s = s + " + " + (coef[i]);

else if (coef[i] < 0)

s = s + " + " + (-coef[i]);

if (i == 1)

s = s + "x";

else if (i > 1)

s = s + "x^" + i;

}

return s;

}

public int compareTo(Polynomial arg) {

if (coef.length != arg.coef.length) {

return arg.coef.length - coef.length;

}

for (int i = coef.length - 1; i >= 0; i--) {

if (coef[i] != arg.coef[i]) {

return arg.coef[i] - coef[i];

}

}

return 0;

}

}

Code:`public class FileUnsorted extends RuntimeException {`

public FileUnsorted() {

System.out.println("File is not sorted.");

}

}

I cannot figure out if I need to look at the checkSorted method, compareTo method, or some other method. I've looked through the code many times, but cannot find the issue(s). I have a feeling its somewhere in one of the loops since I tend to mess those up.

Any guidance and/or direction is greatly appreciated.