Solved

Issue with comparison operators

Posted on 2014-12-15
5
141 Views
Last Modified: 2014-12-15
Hi!!

Ok - So I have an application that where I need to compare the variable "sum" which is an int to a TextView which is a String.

For some reason the comparison isn't working, or the s1 isn't getting the value.

In the case statement, if I just have this line and no if, it works: answer1TextView.setVisibility(View.INVISIBLE);
but I want to compare the values to s1

sum = 0; // code will run in the app and will change the value of sum
	public OnTouchListener answerListener = new OnTouchListener() {
		String s1 = String.valueOf(sum);
		
		 @Override
		    public boolean onTouch(View v, MotionEvent event) {
		        int id = v.getId();
		        switch(id){
		                      case 1:
		                    	  if (answer1TextView.equals(s1))
		                    			  {
		                    		  answer1TextView.setVisibility(View.INVISIBLE);
		                    			  }
		                         break;
		                      case 2:
		                    	  if (answer2TextView.equals(s1))
                    			  {
		                    		  answer2TextView.setVisibility(View.INVISIBLE);
                    			  }
		                         break;
		                      case 3:
		                    	  if (answer3TextView.equals(sum))
                    			  {
		                    		  answer3TextView.setVisibility(View.INVISIBLE);
                    			  }
		                         break;
		                    }
		        return false;
		    }

Open in new window

0
Comment
Question by:Computer Guy
  • 2
  • 2
5 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 250 total points
ID: 40501713
I think you want to call getText() on the view.  Right now you're comparing the view itself to the string - not the text in the view.

So something like:

if (answer1TextView.getText().toString().equals(s1)) {
   ...
}

Doug
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40501722
Hi,

Thanks for the quick reply!

Have a look at the addProblem() method

After some troubleshooting I have determined that the sum is 0.

I want to set the sum at the top of the code (int sum =0;) to the sum after addProblem generates then answer with the numbers, (unless you have a better idea)

import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.content.Intent;

public class MainActivity extends ActionBarActivity {

	private TextView equasionTextView;
	private TextView answer1TextView;
	private TextView answer2TextView;
	private TextView answer3TextView;
	private TextView selectAnswerTextView;
	private RadioButton addRadio;
	private RadioButton subRadio;
	private RadioGroup radioGroup;
	private Button exitButton;
	
	int sum = 0; // sum
	
	private static final int REQUEST_CODE = 10; // for intent

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		radioGroup = (RadioGroup) findViewById(R.id.addSubRadioGroup);
		addRadio = (RadioButton) findViewById(R.id.addRadio);
		subRadio = (RadioButton) findViewById(R.id.subRadio);
		equasionTextView = (TextView) findViewById(R.id.equasionTextView);
		selectAnswerTextView = (TextView) findViewById(R.id.selectAnswerTextView);
		exitButton = (Button) findViewById(R.id.exitB);
		radioGroup.setOnCheckedChangeListener(radioListener);
		
		sum = 0;
		
		// Initialize answer textviews to ""
		answer1TextView = (TextView) findViewById(R.id.answer1TextView);
		answer2TextView = (TextView) findViewById(R.id.answer2TextView);
		answer3TextView = (TextView) findViewById(R.id.answer3TextView);
		
		answer1TextView.setText("");
		answer2TextView.setText("");
		answer3TextView.setText("");
		
		answer1TextView.setId(1);
		answer2TextView.setId(2);
		answer3TextView.setId(3);
		
		selectAnswerTextView.setVisibility(View.INVISIBLE);
		equasionTextView.setVisibility(View.INVISIBLE);
		answer1TextView.setOnTouchListener(answerListener);
		answer2TextView.setOnTouchListener(answerListener);
		answer3TextView.setOnTouchListener(answerListener);
		exitButton.setOnClickListener(exitListener);

	}
	
	// Calculate the addition problem
	private void addProblem()
	{
		Random r = new Random();
		int guess1; // random number for guess 1
		int guess2; // random number for guess 2
		int first = r.nextInt(9) + 1; // 1-9
		int second = r.nextInt(9) + 1; // 1-9
		
		//answer
		sum = first + second;
		
		// generate random numbers for other guesses
		guess1 = r.nextInt(9);
		guess2 = r.nextInt(9);
		
		
		
		String problem = Integer.toString(first) + " + " + Integer.toString(second) + " = ? " ;
		equasionTextView.setText(problem);
		
		String g1 = Integer.toString(guess1);
		String g2 = Integer.toString(sum);
		String g3 = Integer.toString(guess2);
		
		answer1TextView.setText(g1);
		answer2TextView.setText(g2);
		answer3TextView.setText(g3);
		
		// Set equasion & answer text view to visible
		equasionTextView.setVisibility(View.VISIBLE);
		selectAnswerTextView.setVisibility(View.VISIBLE);
		
		
	}
	// Calculate the subtraction problem
	private void subtractProblem()
	{
		Random r = new Random();
		int guess1 = r.nextInt(9) + 1; // random number for guess 1
		int guess2 = r.nextInt(9) + 1; // random number for guess 2
		int first = r.nextInt(9) + 1;
		int second = r.nextInt(9) + 1;
		
		// handle negatives
		if (first < second) {
			int temp = first;
			first = second;
			second = temp;
		}
		
		
		// answer
		sum = first - second;
		
		String problem = Integer.toString(first) + " - " + Integer.toString(second) + " = ? " ;
		equasionTextView.setText(problem);
		
		String g1 = Integer.toString(guess1);
		String g2 = Integer.toString(sum);
		String g3 = Integer.toString(guess2);
		
		answer1TextView.setText(g1);
		answer2TextView.setText(g2);
		answer3TextView.setText(g3);
		
		// Set equasion & answer text view to visible
		equasionTextView.setVisibility(View.VISIBLE);
		selectAnswerTextView.setVisibility(View.VISIBLE);
		
		
		
	}
	public OnCheckedChangeListener radioListener = new OnCheckedChangeListener() {

		@Override

		public void onCheckedChanged(RadioGroup group, int checkedId) {


               if(addRadio.isChecked())
            	{
            	   addProblem();
            	}
               else
                {
            	   subtractProblem();
                }
		


}
	};
	
	// Listeners for answers
	public OnTouchListener answerListener = new OnTouchListener() {
		String s1 = String.valueOf(sum);
		
		 @Override
		    public boolean onTouch(View v, MotionEvent event) {
		        int id = v.getId();
		        switch(id){
		                      case 1:
		                    	  //if (answer1TextView.equals(s1))
		                    	 if (answer1TextView.getText().toString().equals(s1))
		                    			  {
		                    		  answer1TextView.setVisibility(View.INVISIBLE);
		                    			  }
		                         break;
		                      case 2:
			                    	 if (answer2TextView.getText().toString().equals(s1))
	                    			  {
	                    		  answer2TextView.setVisibility(View.INVISIBLE);
	                    			  }
		                         break;
		                      case 3:
			                    	 if (answer3TextView.getText().toString().equals(s1))
	                    			  {
	                    		  answer3TextView.setVisibility(View.INVISIBLE);
	                    			  }
		                         break;
		                    }
		        return false;
		    }


	};

Open in new window

0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 40501799
Line 166 of the above code, (ie. where you are converting the "sum" into a String called s1) will need to be moved down so that it is within the "onTouch" method, ie. move it down to around line 170.

THe problem is that at the moment, line 166 gets executed at startup when you are CREATING the OnTouchListener NOT when the listener actually gets EXECUTED in response to a touch event.
0
 
LVL 3

Author Closing Comment

by:Computer Guy
ID: 40501800
Thanks guys!!
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40501883
You're welcome!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now