I'm a beginner--I'm supposed to write a class that calculates the result of a given math expression in a char array, taking into account precedence with the operators +, -, *, and /. For example:
Input: char[] a = {'1', '1',’1’, '-', '1', '2',‘*’, ‘2’}, Output: 87
All the solutions I can think of involve snarls of if statements or messy nested loops. Any advice? Thanks in advance for any help. Here's what I have so far, by the way:
import java.util.Scanner;
public class MathInCharArray {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("Input a calculation without spaces, using the operators +, -, *, or /: ");
String userInput = scnr.next();
char[] equationArray = new char[userInput.length()];
boolean operator = false;
for (int i = 0; i < userInput.length() - 1; ++i) {
equationArray[i] = userInput.charAt(i);
}
for (int i = 1; i < userInput.length() - 1; ++i) {
for (int j = 0; j < userInput.length() - 2; ++j) {
}
}
}
}