comparing string values - android

I want to compare 2 strings.
My first value is in 'list[0][0]' variable and the second value is in item[0].
But when I am comparing the 2 strings using 'if' statement, I don't get the answer.
if(selected_list[0][0]==items[0])
{
// some code
}
it is not working.
But when I am hard-coded these values, it is working fine.
if("banana"=="banana")
{
// some code
}
Please give me the solution?
Thank you..

Here is an explanation of how strings should be compared and the different options for doing so. They aren't as simple as comparing int's.
if (string1.equals(string2))

You have to compare it as [list[0][0] isEqualToString:items[0]] otherwise you are comparing their addresses not the values.

Use the compareTo() or equals() method of one of your strings, passing the other string as argument.
string1.equals(string2)
// returns true if both strings are equal
string1.compareTo(string2)
// returns 0 if both strings are equal

Related

Java compareTo string based on whole value ( not lexicographically)

The default String compareTo() function compares lexicographically i.e character by character.
E.g. String "3222368" is lesser than "9876135" because 3 < 9.
But i want to compare the whole value based on how swift does it. The sections of importance -
Comparing Strings Using Operators
Comparing strings using the equal-to
operator (==) or a relational operator (like < and >=) is always
performed using the Unicode canonical representation, so that
different representations of a string compare as being equal.
AND
static func <=(String, String)
Returns a Boolean value indicating whether the value of the first
argument is less than or equal to that of the second argument
How can i do this in Java ?
I tried looking up java.text.Collator
and tried this -
Collator myDefaultCollator = Collator.getInstance();
return myDefaultCollator.compare(string1, string2);
and it's not working.
Could you help me out ?
UPDATE:
I'm looking to sort the strings using the compareTo() method.

EditText setText not displaying

In my onCreate() I have:
if(sharedPref.getString("name",null)==""){EditText.setText("Something");}
else{EditText.setText(sharedPref.getString("name",null));}
And then in my onStop() I have:
sharedPrefEditor.putString("name",EditText.getText().toString());
The EditText shows only the hint when I first install and run it. It does seems to display the correct text when it's started later, however.
Don't use == to compare content of Strings. Use equals() instead :
if(sharedPref.getString("name",null).equals("")){
EditText.setText("Something");
}
Why should I use equals instead of ==
First of all you are not using correctly the shared preferences. You are typing null as second parameter which is the value you will get if the key you typed as first parameter is not defined. This is not a problem itself but then in your if sentence you are comparing it to an empty string.
So first of all use equals instead of ==and then if you want to receive an empty string if the key is not defined type an empty string as second parameter of SharedPreferences.gerString method.
Hope it helps ;)
If "Something" is to be used if preference not set, then:
EditText.setText(sharedPref.getString("name","Something"));
getString() will automatically return "Something" if preference is not set. No need of extra code.
String strName = sharedPref.getString("name","");
if(!strName.isEmpty()){
EditText.setText(strName);
}else{
EditText.setText("Something");
}
The operator == can not be used to compare strings in Java.
if(string1.equals(string2){
}
Also, you set a value to SharedPreferences, but you never commit the changes.
sharedPrefEditor.commit();
OnCreate
String prefsVal = sharedPref.getString("name", null);
if(prefsVal.equals(null)){ //If default value was returned
EditText.setText("Something");
}else{
EditText.setText(prefsVal);
}
OR
The following will turn your posted code into a single line solution. The second parameter passed to SharedPref is the default value to be returned if no vale was found for the given key.
EditText.setText(sharedPref.getString("name", "Something");
OnStop
sharedPrefEditor.putString("name", EditText.getText().toString());
sharedPrefEditoy.commit();

how to perform a condition the basis of item selection on two spinners?

I have two spinners in my app. I want that if i select option "first" from spinner 1 and option "second" from spinner 2, then the action gets performed. But it show "NUMBERFORMATEXCEPTION".
Here's the code
if (((spinner.getItemAtPosition(pos).toString()=="first" &&
(s2.getItemAtPosition(id).toString()=="second"))))
{
tv.setText(String.valueOf(gmtomilli(x)));
}
This code has the error, if i omit this code, then the app works fine,without action
Do String comparison using equals.
spinner.getItemAtPosition(pos).toString()=="first"
instead use:
spinner.getItemAtPosition(pos).toString().equals("first")
Similarly for:
s2.getItemAtPosition(id).toString()=="second"
instead use:
s2.getItemAtPosition(id).toString().equals("second")
Read this for more information.
== compares references,not the values. In your case, you want to check for the value equality, not the reference equality.
EDIT:
Since you have mentioned that your code is generating NumberFormatException, I probably believe that either pos or id are of String type generating the NumberFormatException.
EDIT 2:
As per the your comment:
float x=Float.parseFloat(String.valueOf(et.getText()));
Your getText() is returning a String that can't be actually parsed into a float. Try checking if the content is actually a float in String format.
Besides, use String.trim() before parsing to ensure your String doesn't contain any leading or trailing whitespaces that's generating the NumberFormatException.

Determining which value of IntentExtra has been sent

I want to determine which value has been sent by different buttons that I have in my code. I have tried the code below.
Thanks in advance!
if (intent.getExtras().getString("") == button1value){
}
You can't compare two strings with ==.
if (intent.getExtras().getString("").equals(button1value)) {
}

Android math operator

I need calculate a thing. but my formula sentence has occur some problem.
TextView ticketP = (TextView)findViewById (R.id.ticketQ);
ticketP.setText(oneSession.getTicketOder());
String Ctotal = "";
Ctotal = jsonObject.optString("price");
String OneTotal = oneSession.getTicketOder() * Ctotal; // this part has occur the problem which is the operator * .
You'll need to convert the Strings to numeric type before performing any multiplication. Depending on the type of numeric value you are using take a look Double.parseDouble(String string) or Integer.parseInt(String string).
int oneTotal = Integer.valueOf(oneSession.getTicketOder()) * Integer.valueOf(Ctotal);
to convert it again to String use
String.valueOf(oneTotal)
Yes. Your going to have to use the parsing methods in order to convert the string to a native numerical type. You also need to be care about a few things with your code.
json.optString() can return null. opt = optional.
I would suggest using json.getInt() or json.getDouble() this will not only give you the correct type, but also throw an exception if the values aren't correct.
Secondly your going to have to convert your numerical answer back to a string if you want to display it. But this is easy enough with a .toString() or + "" if you are lazy.

Categories

Resources