Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Issue with comparison operators

Posted on 2014-12-15
5
Medium Priority
?
154 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 28

Accepted Solution

by:
dpearson earned 1000 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 36

Assisted Solution

by:mccarl
mccarl earned 1000 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 36

Expert Comment

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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 …
Suggested Courses

971 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