My answer is a little different because I'm assuming that you'll eventually want the operators as well as the values.
Here's the result from one of my test runs.
Original Equation: 1+2*3(30+4/2-(10+2))*2+1
Expanded Equation: 1 + 2 * 3 ( 30 + 4 / 2 - ( 10 + 2 ) ) * 2 + 1
It looks like all I did was add spaces to the equation. What I actually did was divide the equation into parts, and print each part, followed by a space.
Here's the code I wrote to create a List<String> of parts from the original equation.
private List<String> parseEquation(String equation) {
List<String> parts = new ArrayList<>();
StringBuilder builder = new StringBuilder();
String testCharacters = "+-*/()";
char[] letters = equation.toCharArray();
for (int i = 0; i < letters.length; i++) {
if (letters[i] == ' ') {
continue;
}
if (Character.isDigit(letters[i])) {
builder.append(letters[i]);
continue;
}
if (letters[i] == '.') {
builder.append(letters[i]);
continue;
}
if (contains(letters[i], testCharacters)) {
addPart(parts, builder);
parts.add(Character.toString(letters[i]));
}
}
addPart(parts, builder);
return parts;
}
private void addPart(List<String> parts, StringBuilder builder) {
if (builder.length() > 0) {
parts.add(builder.toString());
builder.delete(0, builder.length());
}
}
private boolean contains(char c, String text) {
for (int i = 0; i < text.length(); i++) {
char t = text.charAt(i);
if (c == t) {
return true;
}
}
return false;
}
The addPart method is a utility method that checks for an empty part. If the part is not empty, this method adds the part to the List.
The contains method is a utility method that allows me to check for all the operators and parenthesis with one if statement.
The parseEquation method doesn't use any fancy regexes. It's straightforward.
I check each character in the equation. If the character is a space, I skip the character. If the character is a digit or a dot, I add it to the StringBuilder to create a number. If the character is an operator or parenthesis, I add the previous number to the List of parts, then add the operator or parenthesis to the List of parts.
I use continue statements to skip to the bottom of the for loop and make the code a little neater. I don't have nested if statements with the continue statements.
Here's the complete, runnable code. You can see by the commented line that I wrote an equation solver as well. I'll leave that part of the code to you.
import java.util.ArrayList;
import java.util.List;
public class EquationSolver {
private static final String FORMAT = "%-30s";
public static void main(String[] args) {
String equation = "1+2*3(30+4/2-(10+2))*2+1";
String title = String.format(FORMAT, "Original Equation:");
System.out.println(title + equation);
EquationSolver es = new EquationSolver();
List<String> parts = es.parseEquation(equation);
// es.processEquation(parts);
title = String.format(FORMAT, "Expanded Equation:");
System.out.println(title + es.toString(parts));
}
private List<String> parseEquation(String equation) {
List<String> parts = new ArrayList<>();
StringBuilder builder = new StringBuilder();
String testCharacters = "+-*/()";
char[] letters = equation.toCharArray();
for (int i = 0; i < letters.length; i++) {
if (letters[i] == ' ') {
continue;
}
if (Character.isDigit(letters[i])) {
builder.append(letters[i]);
continue;
}
if (letters[i] == '.') {
builder.append(letters[i]);
continue;
}
if (contains(letters[i], testCharacters)) {
addPart(parts, builder);
parts.add(Character.toString(letters[i]));
}
}
addPart(parts, builder);
return parts;
}
private void addPart(List<String> parts, StringBuilder builder) {
if (builder.length() > 0) {
parts.add(builder.toString());
builder.delete(0, builder.length());
}
}
private boolean contains(char c, String text) {
for (int i = 0; i < text.length(); i++) {
char t = text.charAt(i);
if (c == t) {
return true;
}
}
return false;
}
private String toString(List<String> parts) {
StringBuilder builder = new StringBuilder();
for (String s : parts) {
builder.append(s).append(" ");
}
return builder.toString().trim();
}
}