0

I'm currently working on a school project (a small android game) and so far I've written a code which generates a random equation 2 seconds after the activity InGame is launched and displays it in a textview. Another 5 seconds later, the second equation is generated and displayed in a different textview. Now the user has to decide if the second equation has a bigger result than the first one by either pressing the button bigger or smaller. If it was correct, the next equation should be displayed and it would go on like this until the user decided wrong. Here is my code so far: (Code for the first equation):

// Generate random equation and display it in textview

String[] operationSet = new String[]{"+", "-", "/", "*"};
String equation;
static double doubleAnswer1;
public void start1() {
    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(4)];
        operations.add(operation);
    }

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

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation += numbers.get(i);
        equation += operations.get(i);
    }
    equation += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView_first_equation);
    TextEquation.setText(equation);

    // Evaluate the result of the equation

    double doubleAnswer1 = eval(equation);

    String stringAnswer = Double.toString(doubleAnswer1);

    TextView textAnswer = (TextView)findViewById(R.id.textView4);
    textAnswer.setText(stringAnswer);
}

(Code for second equation (basically same as for first equation except the name of the strings and doubles are different)):

String equation2;
static double doubleAnswer2;
public void start2() {
    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(4)];
        operations.add(operation);
    }

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

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation2 = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation2 += numbers.get(i);
        equation2 += operations.get(i);
    }
    equation2 += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView3);
    TextEquation.setText(equation2);

    // Evaluate the result of the equation

    double doubleAnswer2 = eval(equation2);

    String stringAnswer = Double.toString(doubleAnswer2);

    TextView textAnswer = (TextView)findViewById(R.id.textView_result2);
    textAnswer.setText(stringAnswer);
}

And here is my onCreate code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ingame);

    // Display first equation 2 seconds after the activity is launched

    final Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable() {
        @Override
        public void run() {
            start1();
        }
    }, 2000);

    final Handler handler2 = new Handler();
    handler2.postDelayed(new Runnable() {
        @Override
        public void run() {
            start2();
        }
    }, 7000);

    // Check if user was right or wrong

    final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
    final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
                Log.v("TAG", "you are right");
            } else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
                Log.v("TAG", "you are right");
            } else {
                Log.v("TAG", "you are wrong");
            }
        }
    };

    buttonBigger.setOnClickListener(listener);
    buttonSmaller.setOnClickListener(listener);
}

The app launches correctly and it also displays the first and second equation, but when I press one of the button, it tells me in the logcat you are wrong but I decided 100% correct. However if I debug the app, it tells me that doubleAnswer1 and doubleAnswer2 are both = 0. That's why it all ways tells me 'you are wrong'. I don't know how to fix this, maybe I need to store the doubleAnswer1 and doubleAnswer2 somewhere. I really don't know what to do, so it would really help me if someone has an idea what to do.

If anything is unclear in my question, feel free to ask and I will try to clarify the problem.

Thank you in advance for your help!

6
  • 3
    because you declare doubleAnswer1 and doubleAnswer2 as a local variable inside the method, and never touch the static variable you declared previously. As a result they´ll both stay with theyr default value. Commented Oct 21, 2016 at 9:42
  • And how can I change that? (I'm new to android developing)
    – ztv
    Commented Oct 21, 2016 at 9:45
  • 1
    Btw: that is called shadowing and simply bad practice: you try to never use the same names for different things.
    – GhostCat
    Commented Oct 21, 2016 at 9:45
  • 1
    You simply do not use local variables, but work on the static ones within your class (for the given problem). Of course, in a "real" design using static class wide variables isn't exactly a great idea either.
    – GhostCat
    Commented Oct 21, 2016 at 9:45
  • 1
    @G.M that has nothing to do with android developing, that´s basic java. Commented Oct 21, 2016 at 9:45

1 Answer 1

1

I think your problem lies here:

double doubleAnswer1 = eval(equation);

I did a quick internet search and did not find any native function called eval(). Instead you should look into Script Engine Manager for java:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));

or exp4j which is shown here: Executing math equation in Android

Edit: change the following:

double doubleAnswer1 = eval(equation);

to:

doubleAnswer1 = eval(equation);

Similarly do the same for doubleAnswer2

5
  • I used this one here: stackoverflow.com/questions/3422673/…
    – ztv
    Commented Oct 21, 2016 at 10:17
  • Yes, it evaluates the result of the equations. I think the problem is somewhere in declare doubleAnswer1 and doubleAnswer2.
    – ztv
    Commented Oct 21, 2016 at 10:23
  • Alright so the problem is definitely at the initialization of doubleAnswer1. Instead of re-initializing it within the methods, just declare it once at the start and leave it at that i.e. remove the double before doubleAnswer1 and doubleAnswer2
    – badrobot15
    Commented Oct 21, 2016 at 10:27
  • You, my friend, helped me a lot!! It worked! When I now debug the app, it tells me the values of doubleAnswer1 and doubleAnswer2 and it tells me 'you are right' :D Thank you!
    – ztv
    Commented Oct 21, 2016 at 10:35
  • @G.M happy to help. Do mark the solution as correct
    – badrobot15
    Commented Oct 21, 2016 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.