Android/Java Help

Associate
Joined
28 Feb 2007
Posts
969
Location
Leeds/Sunderland
OK my problem is that the program doesn't like the if statement but I don't know why.

Here's the code:
XML
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
       <string name="app_name">color Test</string>
    
    <string-array name="Colours"> 
    	<item>Red</item> 
   		<item>Green</item>     
    </string-array> 
    
</resources>
Java
Code:
public class colour_test extends Activity {
    /** Called when the activity is first created. */
	Button b1, b2;
	TextView t1, t2, q1; 
	
	 private String ans;	
	 private String[] myString;	 
	 private static final Random rand = new Random();	
	 
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.main);
		 
		 myString = getResources().getStringArray(R.array.Colours); 
		 
		 String ques = myString[rand.nextInt(myString.length)];      
		 String w = myString[rand.nextInt(myString.length)];
		 String a = myString[rand.nextInt(myString.length)]; 
		 String q = myString[rand.nextInt(myString.length)];  
		 String s = myString[rand.nextInt(myString.length)];
        
        q1 = (TextView) findViewById(R.id.question);
        q1.setText("What Colour Is The Word " + ques);
        ////////////////////////////////////
        
        b1 = (Button) findViewById(R.id.Button01);
        b1.setText(q);
        while(w==q)
        {
        	w = myString[rand.nextInt(myString.length)];
        }
        b2 = (Button) findViewById(R.id.Button02);
        b2.setText(w);
        ///////////////////////////////////
        
        t1 = (TextView) findViewById(R.id.red);
        t1.setTextColor(Color.parseColor(a)); 
        while(a==s)
        {
        	s = myString[rand.nextInt(myString.length)];
        }
        t2 = (TextView) findViewById(R.id.green);
        t2.setTextColor(Color.parseColor(s));
        
        ///////////////////////////////////
        ///////THIS WONT WORK/////////////
        /////////////////////////////////
        if(ques == "Red")
        {
        	ans = a;
        }
        else if(ques =="Green")
        {
        	ans=s;
        }
        //////////////////////////////
        
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (b1.getText().equals(ans)) {
                    Toast.makeText(colour_test.this, "Correct", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(colour_test.this, "Incorrect", Toast.LENGTH_SHORT).show();
                }
            }
        });
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (b2.getText().equals(ans)) {
                    Toast.makeText(colour_test.this, "Correct", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(colour_test.this, "Incorrect", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

If i change the text equals to on the buttons to a or s it works but it sets the answer and i want it to be random.
I hope im clear (prob not) and i know the code is scruffy.
 
Ive changed the iff statement to:
if(q1.getText().equals("What Colour Is The Word Red"))
{
ans = a;
}
else if(q1.getText().equals("What Colour Is The Word Green"))
{
ans=s;
}
and it works but is long.
I don't get why it wouldn't work before though.

Id still like a better answer if there is one.
 
Back
Top Bottom