1

I'm working on a school project in Android Studio and I've already asked here how to randomly generate equations, like 10+48*4. Someone suggested me this code to generate the equations:

String[] operationSet = new String[]{"+", "-", "/", "*"};

public void testGenerateRandomEquations() {

Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;

List<String> operations = new ArrayList<>();

for (int i = 0; i < numOfOperations; i++) {
    String operation = operationSet[random.nextInt(3)];
    operations.add(operation);
}

int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();

for (int i = 0; i < numOfNumbers; i++) {
    int number = random.nextInt(Integer.MAX_VALUE) - random.nextInt(Integer.MAX_VALUE);
    numbers.add(number);
}
//Now you've the list of random numbers and operations. You can further randomize
//by randomly choosing the number and operation from those list.
}

But now I don't know how to display the generated equation. How can I display the equation for example in a TextView? Maybe I'm just too dumb to understand but it would be nice if someone could help me :)

Here is the link to the original post: http://stackoverflow.com/a/39960279/6949270

Dharman
  • 30,962
  • 25
  • 85
  • 135
ztv
  • 134
  • 1
  • 9

1 Answers1

2

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.

TDG
  • 5,909
  • 3
  • 30
  • 51
  • This won't work because the String has not been initialized. You should add `String equation = "";` – Antoine Delia Oct 11 '16 at 09:03
  • So now if I want to display the equation I just need to call up the string equation? – ztv Oct 11 '16 at 09:39
  • Yes. The `equation` string containg your text, so you can display it in a `TextView` with the `setText` method. – TDG Oct 11 '16 at 09:44
  • Thank you all lot!! What I've now done is I placed a button and if you press this it generates a random equation :D It works almost perfectly, the only thing I've noticed so far is that none of the equation contains multiplications but I hope I will figure out why and fix it. – ztv Oct 11 '16 at 09:57
  • The solution to the problem with the multiplication is the following: In the code line: `String operation = operationSet[random.nextInt(3)];` You just need to replace the "3" with a "4". :D – ztv Oct 11 '16 at 10:03
  • Is there a way to save the equation in a double? Because I need the equation in a double for a further step... – ztv Oct 14 '16 at 18:09
  • What do you mean? The "equation" is a `String` - just a bunch of characters. `Double` is something completly different. Do you mean "the result of the equation? If so, you'll have to solve it. – TDG Oct 14 '16 at 18:38
  • I need to have the equation in a double, float, int or long because I want to evaluate the result of the equation by using the "abs" command which unfortunately doesn't support strings. So is there a way to store the equation directly in a double, float, int or long? (I tried to convert the string to a double but then there are still the qoutation marks which a double can't contain) Or is there an other way to solve the equation? – ztv Oct 15 '16 at 09:58
  • A quick search gave me that - http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form. I'm sure there're more answers, just search. – TDG Oct 15 '16 at 10:34