This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Why does my "if" statement not fire?
String something = "";
String category = json_data.getString("den");
Log.e("JSON", "category="+category);
if (category == "1"){
something = "Random something";
}
Even if in my logcat I can see JSON:"category=1", the "something" String does not take "Random something" value.
This must be some convention in java?
Please help.
use:
if (category.equals("1")){
something = "Random something";
}
or better way to avoid null pointer exception:
if ("1".equals(category)){
something = "Random something";
}
Also have a look at this link for detail : How do I compare strings in Java?
You need to do string comparison in Java using the .equals method on the String object. The comparison you are doing is only comparing the references not the actual string values.
Example:
if(category.equals("1")){
//do amazing stuff
}
Related
This question already has answers here:
Get Value of a Edit Text field
(13 answers)
Closed 4 years ago.
Why does not
String.valueOf(edittext var name)
retrieve the text from EditText?
That's what Android has provided.
You can just use editText.getText().toString() instead.
Info about String.valueOf(obj)
String class is a Java class, it does not know Android classes like TextView or EditText.
String.valueOf accept EditText or any object because it will be considered to call String.valueOf(Object) method.
From String Documentation internal implementation of String.valueOf(Object) is
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
If you call String.valueOf(editText) It will return you class name like EditText#2a139a55.
Use editTextName.getText().toString() which returns String value
String value = String.valueOf(editText.getText())
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 7 years ago.
A part of the goal in my app is to receive a mathematical expression (e.g. 1 + 1) by string, and convert it to a BigInteger.
My goal is to have an equivalent to:
BigInteger result = new BigInteger("1+1");
// This will throw an exception of invalid BigInteger
Also, ScriptEngineManager class isn't available for Android.
I still cannot find a way to achieve my goal.
Thanks a lot for helping!
You can try :
//If you have to parse only the result
String result = "2";
BigInteger.valueOf(Long.valueOf("2");
//If you have to parse the whole operation
String result = "1+1";
//create an algo to extract each 1 and determine operation
BigInteger bigIntUn = new BigInteger("1");
BigInteger bigIntAnotherUn = new BigInteger("1");
//you have add (+), min(-), multiply(*) (etc...)
bigIntUn.add(bigIntAnotherUn);
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I hope somebody could give me an explaination why the below code wont work:
//Why doesnt this work
String l = myString.substring(cut, lengthLastBtn-1);
String c = myString.substring(cut, lengthLastBtn-1);
if(l==c){
Log.i(TAG, "Correct");
}
//End
//This work!
String l = "hi";
String c = "hi";
if(l==c){
Log.i(TAG, "Correct");
}
// End
// Or if i want the Vars as in the first code i have to use the if statement like this
if(l.contains(c)){
Log.i(TAG, "Correct");
}
//End
So, why cant a compare a string when i have used the substring method on it. I even see in the log for the strings that they are the same, or have the same text at least.
When you use the “==“ operator with String`s, it means a comparison between objects, not the value that objects hold.
In order to compare Strings values , you should use the built-in method equals. The result is true if the String object represents the same sequence of characters.
if(string1.equals(string2)) {
//Match
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I can't seem to get the code within the second if statement to execute. I have logged both values being compared and also ran the debugger and checked them. They are both "a". It always shows the incorrectPasswordDialog. This question seems difficult because it seems as though it should just work but any help is appreciated.
private void logUserIn(AppUser user) {
if (user != null){
Log.d("mPassword: ", mPassword);
Log.d("user.getPassword(): ", user.getPassword());
String userPassword = user.getPassword().toString();
String formPassword = mPassword.toString();
if ( userPassword == formPassword ){
Intent welcomePage = new Intent(this, StartScreenActivity.class);
welcomePage.putExtra("name", mName);
startActivity(welcomePage);
}
else {
showIncorrectPasswordDialog();
}
}else {
showIncorrectUserNameDialog();
}
}
You are comparing the object identity. Use string.equals() to check for equivalence.
if(userPassword.equals(formPassword)){
}
Change
if ( userPassword == formPassword ){
to
if ( userPassword.equals(formPassword) ){
== compares object references, whereas .equals compared String values.
You can not compare string using ==
try like this
Compares the specified object to this string and returns true if they are equal. The object must be an instance of string with the same characters in the same order.
if ( userPassword.equals(formPassword)){
// do some stuff
}
or
Compares the specified string to this string ignoring the case of the characters and returns true if they are equal.
if(userPassword.equalsIgnoreCase(formPassword))
{
//do some stuff
}
In JAVA to compare between strings, you should use:
if(userPassword.equals(formPassword)) {
// they are equal
}
Change
if ( userPassword == formPassword ){
to
if ( userPassword.equals(formPassword)){
In Java, String comparison is done with .equals(). Using == compares the object reference and not their values.
Java String comparison
In JAVA you should use equals to judge equal of value of different String.
== is used to judge object pointer for string, so it would only return false in your case.
This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 9 years ago.
I need to display the calling methods details like line number, method name and class name.
How to get all those information in android,whenever a method is called in an application the calling method info has to be dispalyed,can anyone help me in solving this...
You can get using the following code- [copied from How to get method name for debug output in Android/Java? ]
Thread current = Thread.currentThread();
StackTraceElement[] stack = current.getStackTrace();
for(StackTraceElement element : stack)
{
if (!element.isNativeMethod()) {
String className = element.getClassName();
String fileName = element.getFileName();
int lineNumber = element.getLineNumber();
String methodName = element.getMethodName();
}
}
Once you got the line number, method name, class name you can use it as you wish.