I had made a fully functioning method that would grab a variable string like x or var for the equation as so:
// Equation Variable Getter
public String getVar(){
String variable = "";
for (int i = 0; i < getEquation().length(); i ){
if ((getEquation().charAt(i) >= 65 && getEquation().charAt(i) <= 90)
|| (getEquation().charAt(i) >= 97 && getEquation().charAt(i) <= 122)){
variable = getEquation().charAt(i);
}
}
//System.out.println(variable);
return variable;
}
While attempting to solve an equation like x = x or num = num, I split the sides into an array and trimmed the whitespaces like any other equation that I had tested. The problem was in comparing the two sides. Naturally, I used an if statement and compared them like so:
if (sides[0].contains(getVar()) && sides[1].contains(getVar()){
// statements used to solve equation format co*var = co*var
}
When using this on x = x and var = var, the if statement never ran. Upon further investigation, I found out that the code did went around it because both of the conditions in the statement were false. This was not supposed to happen as, for strings like x = x and var = var, both did contain the variable x
Is there something wrong with the if statement?
CodePudding user response:
First, I need to thank the people that have assisted with finding the solution to this problem. Apparently, there was, in fact, an issue with the getVar() method getting double variables from equation strings with multiple instances of the variable.
What I did to resolve the issue was to overload the getVar() method to take in and use string parameters so that I can get the variable from just one side of the equation.
The solve() method, which contains the if statement, worked to solve equations that would have 1 or more variables on either side. So, in the cases through which only 1 side would contain the variable, the both getVar() methods would still be used to avoid an IndexOutOfBoundsException.