Solved

Issue with comparison operators

Posted on 2014-12-15
5
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 28

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 36

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 36

Expert Comment

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

622 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