G.M G.M - 1 month ago 17
Java Question

Display the generated equation

Deleted question: This question was deleted by G.M because it was already answered

TDG TDG
Answer

All you have to do is to loop over the two lists - numbers and operations and concatenate them into a single string:

String equation = null;
for (int i = 0; i < numOfOperatrions; i++) {
    equation += numbers.get(i);
    equation += operations.get(i);
}
equation += numbers.get(numbers.size() -1);    //Add the last number to the equation

The last line is needed because there are more numbers (one more) than operations.
It is better to use StringBuilder than String when concatenating strings, but for short strings it's OK.
EDIT - Why does it work?
We are concatenating String with an Integer, using the + operator.
The JAVA compiler converts in this case the Integer into a String, so no other casting is required.

Comments