Solved

Issue with comparison operators

Posted on 2014-12-15
5
136 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys!!
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
You're welcome!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 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

13 Experts available now in Live!

Get 1:1 Help Now