Comparing strings that use the substring method [duplicate] - android

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
}

Related

How to check whether String already exist in Arraylist? [duplicate]

This question already has answers here:
How does a ArrayList's contains() method evaluate objects?
(10 answers)
Closed 5 years ago.
I have an app in which contain ArrayList of some String let suppose a,b,c,d and i have a String called pack. What i have to do is that i have to check whether pack is already exist in arraylist or not if not exist then i have to add it in arraylist. How do i do that
code:-
ArrayList<CBlackList> appsListDataSet;
public void setAppsDetails(String pack) {
if (appsListDataSet != null && appsListDataSet.size() > 0) {
for (int i = 0; i < appsListDataSet.size(); i++) {
CAppsModel appsModel = appsListDataSet.get(i);
if (pack.equalsIgnoreCase(appsModel.getPackageName())){
//Not to know what to do here.
}
}
}
}
Use the contains method in an arrayList. If your arrayList contains only String and you want to check for String value itself, then just do
boolean isPackPresent = appsListDataSet.contains(pack)
this will work if your arraylist has the same datatype as pack and is not a custom object.
Try Some thing like this
if(appsListDataSet.contains("your String"))
{
///Action to be occur
}

How to convert a string mathematical expression into a BigInteger? [duplicate]

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);

"==" for Strings always returning false [duplicate]

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.

convert string array into integer array android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I created a url string array.
String urls[]={"http://www.kllhjj.png","http://yui.kl.png"};
this url are not exactly correct. But in my code them are correct. Then I try to convert them into integer array just like following way.
int a[] = new int[urls.length];
for(int i=0; i<a.length; i++) {
try {
a[i] = Integer.parseInt(urls[i]);
}
catch(Exception e) { }
}
But here always show the a integer values as 0. why is it?
help me
Because Integer.parseInt(urls[i]); is throwing NumberFormatException and you are swallowing the Exception . The below code will not work in your case, but at least you will get to know the error:
try{
a[i]=Integer.parseInt(urls[i]);
}catch(Exception e){
e.printStackTrace();
throw new RunTimeException(e);
}
All the elements of a primitive int array are defaulted to 0. Hence you get the 0s .
You cannot parse string like "http://yui.kl.png" etc to int as they are not in numeric format . Read the documentation:
Throws:
NumberFormatException - if the string does not contain a parsable integer.
You are getting the NumberFormatException Exception hence not changing any values in a[] and because the default values of integer is 0 you are having 0 for every element in a[]
Urls are text string and you parsing text to int:
a[i]=Integer.parseInt(urls[i]);
which through an exception absorbed by:
catch(Exception e)
and default value for int is zero so u always getting it.
ParseInt is a function that search for integer in some string. it's like atoi if you're familiar with c language.
When you have a string that contains only letters it will return 0.
When you have a string that begins with letter and ends with number it also will return 0.
It will convert a string to integer if the string will contains a number characters like "0123".
I don't know what you're trying to do, but if you want that number will represent a url.
I think you can create an enum for the urls.

Android if statement does not fire? [duplicate]

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
}

Categories

Resources