SOLVED
My Exception is thrown at Main.java:42 and I have racked my brain trying to figure out how to fix it
Output
null Strong Ordered: true Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:440) at Main.checkWeakOrder(Main.java:42) at Main.processPolyList(Main.java:64) at Main.main(Main.java:10)
PolyFile.txt
4.0 3 2.5 1 8.0
5.0 4 5.0
4.0 4 5.7 2 8.6
Main.java
import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class Main { private static List<Polynomial> polyList = new ArrayList<>(); public static void main(String[] args) { processPolyList(); } public static ArrayList<String> fromFile(){ ArrayList<String> expressionList = new ArrayList<String>(); JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); int response = chooser.showOpenDialog(null); if (response == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); try { Scanner fileIn = new Scanner(file); if (file.isFile()) { while (fileIn.hasNext()) { String expression = fileIn.nextLine(); expressionList.add(expression); } } } catch (NoSuchElementException nse) { JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "File is empty"); } catch (FileNotFoundException fne) { JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "File not found"); } } return expressionList; } public static boolean checkWeakOrder(List<Polynomial> polyList) { boolean isWeakOrder = true; Polynomial previous = polyList.get(polyList.size()-1); for(int i = polyList.size()-2; i > 0; i--) { if (previous.compareExponents(polyList.get(i)) < 0) { isWeakOrder = false; } } return isWeakOrder; } public static void processPolyList() { try { ArrayList<String> a = fromFile(); for(String element :a) { Polynomial p = new Polynomial(element); System.out.println(p); polyList.add(p); } } catch (InvalidPolynomialSyntax ex) { JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), ex.getMessage()); } System.out.println("Strong Ordered: " + OrderedList.checkSorted(polyList)); System.out.println("Weak Ordered: " + checkWeakOrder(polyList)); } }
Polynomial.java
import java.util.*; public class Polynomial implements Iterable<Polynomial.Term>, Comparable<Polynomial> { Comparator<Polynomial> compare; private Term head; public Polynomial(String fromFile) { head = null; Scanner termScanner = new Scanner(fromFile); try { while(termScanner.hasNext()) { addTerm(termScanner.nextDouble(), termScanner.nextInt()); } } catch(Exception ex) { System.out.println(ex.getLocalizedMessage()); throw new InvalidPolynomialSyntax("Incorrect Syntax, please check inputs!"); } } public void addTerm(double coefficient, int exponent) { if(exponent < 0) { throw new InvalidPolynomialSyntax("No negative exponents, please check inputs!"); } Term current = head; if(current == null) { head = new Term(coefficient, exponent); head.next = null; } else { while(current.next != null) { current = current.next; } current.next = new Term(coefficient, exponent); } } public int compareTo(Polynomial otherPoly) { Term thisCurrent = this.head; Term otherCurrent = otherPoly.head; while(thisCurrent != null && otherCurrent != null) { if(thisCurrent.getExponent() != otherCurrent.getExponent()) { return thisCurrent.getExponent() - otherCurrent.getExponent(); } else if(thisCurrent.getCoefficient() != otherCurrent.getCoefficient()) { if(otherCurrent.getCoefficient() > thisCurrent.getCoefficient()) { return -1; } else if(otherCurrent.getCoefficient() < thisCurrent.getCoefficient()) { return 1; } } thisCurrent = thisCurrent.getNext(); otherCurrent = otherCurrent.getNext(); } if(thisCurrent == null && otherCurrent == null) { return 0; } if(thisCurrent == null) { return -1; } else { return 1; } } public int compareExponents(Polynomial poly2) { Term thisPolyTerm = this.head; Term otherPolyTerm = poly2.head; while(thisPolyTerm != null && otherPolyTerm != null) { if(thisPolyTerm.getExponent() != otherPolyTerm.getExponent()) { return thisPolyTerm.getExponent() - otherPolyTerm.getExponent(); } else { thisPolyTerm = thisPolyTerm.getNext(); otherPolyTerm = otherPolyTerm.getNext(); } } if(thisPolyTerm == null && otherPolyTerm == null) { return 0; } if(otherPolyTerm == null) { return 1; } else { return -1; } } public Polynomial() { compare = (Polynomial poly1, Polynomial poly2) -> poly1.compareExponents(poly2); } public Polynomial(Comparator<Polynomial> compare) { this.compare = compare; } public Iterator<Term> iterator(){ return new Iterator<Term>() { private Term current = getHead(); public boolean hasNext() { return current != null && current.getNext() != null; } public Term next() { Term data = current; current = current.next; return data; } }; } public String toString() { StringBuilder expressionBuilder = new StringBuilder(); if(head.coefficient > 0) { expressionBuilder.append(head.toString()); } else { expressionBuilder.append(" - ").append(head.toString()); } for (Term tmp = head.next; tmp != null; tmp = tmp.next) { if(tmp.coefficient < 0) { expressionBuilder.append(" - ").append(tmp.toString()); } else { expressionBuilder.append(" + ").append(tmp.toString()); } } return expressionBuilder.toString(); } static class Term { private double coefficient; private int exponent; private Term next; private Term(double c, int e) { coefficient = c; exponent = e; next = null; } private int getExponent() { return this.exponent; } private double getCoefficient() { return this.coefficient; } private Term getNext() { return next; } public String toString() { String termString = String.format("%.1f", Math.abs(coefficient)); if(exponent == 0) { return termString; } else if(exponent == 1) { return termString + "x"; } else { return termString + "x^" + exponent; } } } private Term getHead() { return head; } }
OrderedList.java
import java.util.*; public class OrderedList { public static <T extends Comparable<? super T>> boolean checkSorted(List<T> list) { boolean isSorted = true; for(int i = list.size()-1; i > 0; i--) { T current = list.get(i); if(!checkSorted(list, current)) { isSorted = false; } } return isSorted; } private static <T extends Comparable<? super T>> boolean checkSorted(List<T> list, T current){ T currentValue = list.get(list.indexOf(current)); T nextValue = list.get(list.indexOf(current)- 1); if(nextValue != null) { return currentValue.compareTo(nextValue) >= 0; } return true; } }
InvalidPolynomialSyntax.java
public class InvalidPolynomialSyntax extends RuntimeException { InvalidPolynomialSyntax(String msg){ super(msg); } }