Solved

Issue with comparison operators

Posted on 2014-12-15
5
147 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 27

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 51
ejb wildfly example 2 30
Is Applet the way to go for my drag and drop system? 8 25
Setup GlassFish 4 22
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

807 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