I am making a currency converter with two spinners. I want to make an "if" function using the values of the spinner's selected item like below.
#Override
public void onClick(View v) {
if (spinner1.getSelectedItem()=="Dollars" && spinner2.getSelectedItem()=="Euros") {
convertDollarstoEuros();
}
if (spinner1.getSelectedItem()=="Euros" && spinner2.getSelectedItem()=="Euros") {
convertEurostoEuros();
}
Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
The problem is that the toast is showing, but the currencies aren't converting. The toast part is working, but the spinner part isn't. Any help would be greatly appreciated. Here is my LogCat:
Try this :
if (spinner1.getSelectedItem().toString().equals("Dollars") && spinner2.getSelectedItem().toString().equals("Euros")
...
getSelectedItem() returns an Object . info . So you have to get the corresponding string first.
Then java compares strings using equals().
if (spinner1.getSelectedItem()=="Dollars" && spinner2.getSelectedItem()=="Euros") {
You can't compare Strings like that. You have to use the equals() method to compare them. Use this:
if (spinner1.getSelectedItem().toString().equals("Dollars") && spinner2.getSelectedItem().toString().equals("Euros")) {}
Check whether you called the id correctly or not? When I was working when improper id was called this exception used to arise. For eg In TextView, findViewById(R.id.textView1) but in xml file we may have set it to textView2.
Related
Im trying to make an app that converts the value that the user inputs into the editText. But when I run the program it shuts down and won't show the input the user inputted or the input divided by 2. I tried putting btCalculate.setText("Hi" + convert); and that displayed hi and the user input but when I get rid of the string and just have convert it shuts down. Can someone help me or make sense of what I'm trying to do?
Here is my code:
btCalculate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
int convert = Integer.parseInt(editText.getText().toString());
if (btCopper1.isPressed());
btCopper2.isPressed();{
btCalculate.setText(convert);
}
if (btCopper1.isPressed());
btSilver2.isPressed();
btCalculate.setText(convert/2);
}
});
}
}
Is a good practice to code as simple as possible. It will be easier to found and fix issues.
Exemple:
if(expression == true) {
// do this instructions
}
In your code, start fixing the {}, making it easier to understand:
if (btCopper1.isPressed()) {
btCalculate.setText(convert);
}
Think about it, it seems the error is in your logic.
Basically I need to check that the user input from inputET (an EditText) is equal to the integer, correctAnswer. The problem I'm getting is that "" (which is the text in the EditText field) cannot be converted to an int. Is there any other ways of achieving this or catching the error, I've tried the following code which to my understanding asks if the string in the EditText is not equal to "". Am i going the right way about this or is there an easier way?
// check the input
if (inputET.getText().toString() != "") {
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
if the user inputs the same int as the correctAnswer integer then the EditText text is reset to "".
Thanks for any help.
try this:
if (!inputET.getText().toString().equals("")) {
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
Used .equals() method for String comparison.
Based on your requirement I think using TextUtil class will be right way to go for checking the edittext is empty or not.
if(!TextUtils.isEmpty( inputET.getText().toString())){
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
rather tha doing if (inputET.getText().toString() != "") have a try with
if (!inputET.getText().toString().equals(""))
print the "inputET.getText().toString()" to console and see what it returns. I would hope you need to check the following
String str = inputET.getText().toString()
if (null!=str && str.trim().equals("")) {
if(inputET.getText().toString()!=null&&!(inputET.getText().toString().isEmpty())){
//code for mot null
}else{
//code for null
}
can someone please help making pairs game and using this if statement can someone tell me if theres a bracket or semi colon missing cant see whats missing (pic2.getTag() == beck) is underlined have 4 buttons want to pair or reset them the buttons start with set tag name (boots) this works but no good for non match
This works
if (pic2.getTag() == pic1.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
THis doesnt
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setTag(beck);
pic1.setBackgroundResource(R.drawable.becks);
if (pic2.getTag() == beck) {
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);
}
}
});
The problem is in the compare operator ==. View tags are Objects (most probably Strings), so you need to compare them with equals.-
if (pic2.getTag().equals(pic1.getTag()))
and
if (pic2.getTag().equals(beck))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm having a problem with a simple if statement and i'm not too sure why. I've got an edittext box and a button, when the user inputs a value into the edittext and then presses the button, whatever was input into the box is converted to string and stored in a variable, this variable is then displayed in a toast. Now this works perfectly fine as it is but I would like it to only display if a certain value is input into the editbox but when I put in an if statement to validate this, it seems to completely disgregard the if statement and does nothing. It does not cause any errors but it stops any toast from being displayed even if the correct string is input. I'm sure this is something simple but I can't seem to work it out. It would be great if anyone could work out why it does this.
Code below:
Working code when the if statement is commented out:
saveBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Global.controlNum = inputTxt1.getText().toString();
// if((Global.controlNum == "1")||(Global.controlNum == "2" )){
Toast toast= Toast.makeText(SettingsScreen.this,"hello " + Global.controlNum, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, -100);
toast.show();
// }
}
});
if the if statement is brought in then it will do nothing:
saveBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Global.controlNum = inputTxt1.getText().toString();
if((Global.controlNum == "1")||(Global.controlNum == "2" )){
Toast toast= Toast.makeText(SettingsScreen.this,"hello " + Global.controlNum, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, -100);
toast.show();
}
}
});
Read about How do I compare strings in Java?
So Simply change
if((Global.controlNum == "1")||(Global.controlNum == "2" ))
With
if(("1".equals(Global.controlNum))||("2".equals(Global.controlNum) ))
you should change it with:
if((Global.controlNum == "1")||(Global.controlNum == "2" ))
to
if((Global.controlNum.equals("1"))||(Global.controlNum.equals("2") ))
You should use equlas() to compare String
Global.controlNum.equlas("1")
Try replacing
if((Global.controlNum == "1")||(Global.controlNum == "2" ))
with
if((Global.controlNum.equalsIgnoreCase("1"))||(Global.controlNum.equalsIgnoreCase("2") ))
Use Global.controlNum.equals("1")
If you want to compare Strings in Java (Android) always use the method equals(String) or equalsIgnoreCase(String)
Sorry if it's a silly question. I need to compare with a name which has three words separated by one white space. If the name is null or "This is Android" i would do something, otherwise i do something else. For example, is the following code right to do this comparison?
if((name==null)||(name.equalsIgnoreCase("This is Android")))
{
//start activity 1
}
else
{
//start activity 2
}
"This is Android " is different from "This is Android" and equalsIgnoreCase will return false. You can use trim() to remove spaces and the start or the end of the Strings.
Hope this helps!
You should check if the name is null before you do that, otherwise it looks good. (except for, it should be if instead of If):
//either
if(name != null) {
if(name.equalsIgnoreCase("This is Android") {
}
}
//or
if("This is Android ".equalsIgnoreCase(name)) {
Update:
When you are comparing strings, the whitespaces count. So, basically "Hello world" and "Hello world " are not equal.
You need to use the .trim() method to ignore the surrounding whitespaces.
name = name.trim(); //since strings are immutable you need to assign return value to name
if("This is Android".equalsIgnoreCase(name)) {
Always keep constant String on left hand side in equals, this ensures no NPE:
like this :
if ("This is Android ".equalsIgnoreCase(str1)) {
// start activity 1
} else {
// start activity 2
}
In case you dont want space then add trim():
if ("This is Android ".trim().equalsIgnoreCase(str1)) {
// start activity 1
} else {
// start activity 2
}
if("This is Android".equalsIgnoreCase(name))
// start activity 1
} else {
// start activity 2
}
or the bullet-proof (in case user pasted value with unwanted spaces at the end of string)
if(name != null && "This is Android".equalsIgnoreCase(name.trim()))
// start activity 1
} else {
// start activity 2
}