0

I am making a multiplication app for kids on Android. Here is the code I have:

import java.awt.*;
import java.math.*;
import javax.swing.*;
public class S1P4 extends JFrame
{


public static void main(String[] args){
    int l = 0;
    int x =(int)(Math.random()*100);
    int y =(int)(Math.random()*10);
    int answer = -1;
    String answer3 = null;
    int []anArray;
    anArray = new int[20];



    for ( int i=0; i < 20; i++)
    {
        if ( (i % 2)==0)
            anArray[i]=(int)(Math.random()*100);
        else
            anArray[i]=(int)(Math.random()*10);
    }

    String answerString = "";
    String rightAnswer = "";
    for (l=0; l<20; l++) 
    {
        rightAnswer = Integer.toString(anArray[l] * anArray[l+1]);
        answerString = (JOptionPane.showInputDialog("The Problem is " + anArray[l] + " * " + anArray[l+1] + ", What is the answer? "));
    while (answerString.equals(""))
        answerString =JOptionPane.showInputDialog("Please type an answer to the problem:  " + anArray[l] + " * " + anArray[l+1] + ", What is the answer? "); 
    while (!answerString.equals(rightAnswer))
        answerString =JOptionPane.showInputDialog("Good try! but that's not it.. The problem again is:  " + anArray[l] + " * " + anArray[l+1] + ", What is the answer? ");                  
    }
    for (int n=0; answerString != rightAnswer; n++){
        if (anArray[l] == 1){
        System.out.println("Congratulations! you got all the correct answers in "+ n + l +"tries!");
        System.exit(0);}
        else{
            System.out.println("Error.");
            }
    }
}

Why won't this code work? I want it to print how many tries it took the user to get all the multiplication problems correct.

for (int n=0; answerString != rightAnswer; n++){
    if (anArray[l] == 20){
    System.out.println("Congratulations! you got all the correct answers in "+ n + l +"tries!");
    System.exit(0);}
    else{
        System.out.println("Error.");
        }
  }
}
E25
  • 11
  • 1
  • 5
  • You need to explain what you mean by "won't work". – arshajii Aug 18 '13 at 21:06
  • "Why won't this code work?" - What's the problem, what happens when you run it? – Andrew Martin Aug 18 '13 at 21:07
  • I want the code at the end of my post to print how many tries it took whoever used the program to get all the answers correct. It however terminates the program and doesn't print anything. – E25 Aug 18 '13 at 21:24
  • You can't use Swing JFrames and have a main method on android. Read an introductory android tutorial. – JB Nizet Aug 18 '13 at 21:49

2 Answers2

3

You should use equals() to compare strings:

for (int n=0; !answerString.equals(rightAnswer); n++){

!= compares references, not the actual string objects. Also, a while-loop seems better suited for this:

int n = 0;
while (!answerString.equals(rightAnswer)) {
    ...
    n++
}

Generally, when the loop control variable has nothing to do with the loop condition, it's better to use a while-loop.

Additionally, if you want to take null cases into account when comparing the strings, consider using TextUtils.equals().

See also:

Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • On a side note, a while loop would surely be better than a for loop. For loops are for a particular number of iterations, while loops are used as long as a condition isn't met. – Andrew Martin Aug 18 '13 at 21:11
  • I'll try using a while loop and changing != to equals() I'll update if it works or not. – E25 Aug 18 '13 at 21:15
  • @user2399630: Use this for guidance: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html – Andrew Martin Aug 18 '13 at 21:18
  • It still doesn't work, I am not sure why. – E25 Aug 18 '13 at 21:38
  • 1
    @user2399630 You have to explain what you mean by "doesn't work". – arshajii Aug 18 '13 at 21:39
  • **Don't use String.equals on Android!** TextUtils.equals(string1, string2) is the way to go! – Leandros Aug 18 '13 at 22:22
  • @Leandros That's hardly a reason to downvote. Anyway, I added it to the answer. – arshajii Aug 18 '13 at 22:32
  • Well, you're right. Undone. Nevertheless, it's hardly encouraged by the Android engineers to prefer the TextUtils class over String.equals, but it's only useful if you may encounter a `null` or compare CharSequences as well. – Leandros Aug 18 '13 at 22:33
0

You don't keep track of how many times the user tried to get the right answer. In while (!answerString.equals(rightAnswer)) {…} you need to increment a counter for the number of tries.

The final for() loop does nothing useful. It uses n as the loop counter, but checks anArray[l] (l at this point will be 20, which is an undefined index in your array -- you only allocate 20 values).

Finally, you have an off-by-one error. You are trying to calculate anArray[l] * anArray[l+1], but when l==19, l+1 will be 20, and you will try to access one past the end of the array. You need to initialise 21 values, but only loop 20 times, for this method to work.

Logan Pickup
  • 2,294
  • 2
  • 22
  • 29