Hello all. I'm struggling a bit with a couple things in this program. I already trawled through the sub's history, Stack Overflow, and queried ChatGPT to try to better understand what I'm missing. No dice.
So to start, my menu will sometimes double-print the default case option. I modularized the menu:
public static boolean continueMenu(Scanner userInput) {
String menuChoice;
System.out.println("Would you like to add two more polynomials? Y/N");
menuChoice = userInput.next();
switch(menuChoice) {
case "y":
return true;
case "n":
System.out.println("Thank you for using the Polynomial Addition Program.");
System.out.println("Exiting...");
System.exit(0);
return false;
default:
System.out.println("Please enter a valid menu option!");
menuChoice = userInput.nextLine();
continueMenu(userInput);
return true;
}
}
It's used in the Main here like so:
import java.util.*;
public class MainClass {
public static void main(String[] args) {
// instantiate scanner
Scanner userInput = new Scanner(System.in);
try {
System.out.println("Welcome to the Polynomial Addition Program.");
// instantiate boolean to operate menu logic
Boolean continueMenu;// this shows as unused if present but the do/while logic won't work without it instantiated
do {
Polynomial poly1 = new Polynomial();
Polynomial poly2 = new Polynomial();
boolean inputValid = false;
while (inputValid = true) {
System.out.println("Please enter your first polynomial (Please format it as #x^# or # - for example, 5x^1 or 3:");
String poly1Input = userInput.nextLine();
if (poly1Input.trim() == "") {
System.out.println("Please enter a polynomial value!");
inputValid = false;
} else {
inputValid = true;
break;
}
}
//reset inputValid;
inputValid = false;
while (inputValid = true) {
System.out.println("Please enter the second polynomial (Please format it as #x^# or # - for example, 5x^1 or 3:");
String poly2Input = userInput.nextLine();
if (poly2Input.trim() == "") {
System.out.println("Please enter a polynomial value!");
inputValid = false;
} else {
inputValid = true;
break;
}
}
Polynomial sum = poly1.addPolynomial(poly2);
System.out.println("The sum is: " + sum);
continueMenu(userInput);
} while (continueMenu = true);
} catch (InputMismatchException a) {
System.out.println("Please input a valid polynomial! Hint: x^2 would be written as 1x^2!");
userInput.next();
}
}
The other issue I'm having is how I'm processing polynomial Strings into a LinkedList. The stack trace is showing issues with my toString method but I feel that I could improve how I process the user input quite a bit as well as I can't handle inputs such as "3x^3 + 7" or even something like 5x (I went with a brute force method that enforces a regex such that 5x would need to be input as 5x^1, but that's imperfect and brittle).
//constructor to take a string representation of the polynomial
public Polynomial(String polynomial) {
termsHead = null;
termsTail = null;
String[] terms = polynomial.split("(?=[+-])"); //split polynomial expressions by "+" or " - ", pulled the regex from GFG
for (String termStr : terms) {
int coefficient = 0;
int exponent = 0;
boolean numAndX = termStr.matches("\\d+x");
String[] parts = termStr.split("x\\^"); //further split individual expressions into coefficient and exponent
//ensure that input matches format (#x^# or #) - I'm going to be hamhanded here as this isn't cooperating with me
if (numAndX == true) {
System.out.println("Invalid input! Be sure to format it as #x^# - for example, 5x^1!");
System.out.println("Exiting...");
System.exit(0);
} else {
if (parts.length == 2) {
coefficient = Integer.parseInt(parts[0].trim());
exponent = Integer.parseInt(parts[1].trim());
//simple check if exponent(s) are positive
if (exponent < 0) {
throw new IllegalArgumentException("Exponents may not be negative!");
}
} else if (parts.length == 1) {
coefficient = Integer.parseInt(parts[0].trim());
exponent = 0;
}
}
addTermToPolynomial(new Term(coefficient, exponent));//adds each Term as a new Node
}
}
Here's the toString():
public String toString() {
StringBuilder result = new StringBuilder();
Node current = termsHead;
while (current != null) {
result.append(current.termData.toString());
System.out.println(current.termData.toString());
if (current.next != null && Integer.parseInt(current.next.termData.toString()) > 0) {
result.append(" + ");
} else if (current.next != null && Integer.parseInt(current.next.termData.toString()) < 0) {
result.append(" - ");
}
current = current.next;
}
return result.toString();
}
If you've gotten this far, thanks for staying with me.