I have made a custom ArrayAdapter,I am getting an arrayList in taht adapter,I want to put a condition that two values from arrayList(payer_id and Payee_id) are equal or not,If equal print "equal",Else "Not qual"..My Code is as below:but its all time goes to not equal condition..Please help me save me.
code
public ArrayList<HashMap<String, String>> receivePaymentArray;
if (receivePaymentArray.get(paramInt).get(Const.TAG_PAYEE_ID).equalsIgnoreCase(receivePaymentArray.get(paramInt).get(Const.TAG_PAYER_ID))) {
System.out.println("::::::::::::::::::::::SAME IDS::::::::::::::::");
} else {
System.out.println("::::::::::::::::::::::different ids:::::::::::::::");
}
Are the variables you are comparing strings or integers.. id s are usually int but your question implies they are strings..
Strings compared using == will always return false because they are not the same object.. with strings you have to use '.equals("something")
As you say they are strings try doing
get(Const.TAG_PAYEE_ID).toString().trim().equals(get(Const.TAG_PAYEE_ID).toString().trim())
might solve your issue. Just added .toString().trim() to both parameters get statements.
Related
I am wondering if there is a way to use compareTo() without having to iterate through each string element in the data set, I'm pretty sure this is not possible using arrays, but is there a data structure that is capable of working in that way?
See example below for clearer explanation:
public static int PronounDetector(String [] pronouns)
{
String [] you = {"you", "You"};
for (int i = 0; i < pronouns.length; i++)
{
if (pronouns[i].compareTo(you) == 0)
//Is there a way for compareTo to run through
//the entire String data set without having to make
//it iterate through each element using a for loop?
{
return 2;
}
}
}
EDIT: I understand that no matter what the program will iterate through the data set, (how else will it find a match?), I am just looking to see if there is a way to do it without me actually having the physically type in the for loop.
The must be meet two conditions if you want to skip some data during search processing.
The data must be related.
The data must be organized.
You can improve your search, by sorting the array and then compare from the middle.
Then in each step you will reduce element that must be compare by half.
Instead of array you can used TreeMap, that will store the data in tree structure to have same result.
Code example:
public static boolean contains(String[] array, String key) {
Objects.requireNonNull(array,"The array must not be null");
Objects.requireNonNull(array,"The key must not be null");
String[] copy = array.clone();
Arrays.sort(copy);
return Arrays.binarySearch(copy, key) != -1;
}
Yes, you don't have to "double iterate" (even though that's exactly what happens under the hood) you can convert the array you to a string and search it using contains():
String youStr = Arrays.deepToString(you);
System.out.println(youStr.contains(pronouns[0])); // prints 'true'
I have a List<String> theList
which has following kind of values
"2011-05-05|~JKED"
"2011-05-06|~ABC"
"2011-05-01|~XYZ"
"2011-05-01|~WWX"
As you could guess there are two "fields" in theList.
I want to sort theList on first field and then on second field such as I get following output after sorting operation
"2011-05-01|~WWX"
"2011-05-01|~XYZ"
"2011-05-05|~JKED"
"2011-05-06|~ABC"
If I take these two fields in a separate lists and do Collections.sort(field1List) Collections.sort(field2List) I get the desired output.
But, I want to know, how to use Collections.sort(theList, new Comparator(){}) to be able to sort above theList to get desired output. If it is not possible to solve through Comparator(), please suggest some method which might look like sortMultiFieldList(List<String> theList)
It is a long story why I have to have two or more fields in a single List.
Let me know if you need more clarification.
This is remarkably straightforward. You'll want to write a custom Comparator for this, and enforce its comparison logic to behave the way you want with respect to your two separate "fields".
The motivation here is that these fields are lexicographically compared to one another for the date portion, as well as the alphabetical string portion. If you find that the date comparison isn't giving you accurate results (and it may not; I'm not sure of any cases that it wouldn't work off hand, though), then convert it to a Date and compare that in-line.
Collections.sort(entries, new Comparator<String>() {
#Override
public int compare(String left, String right) {
String[] leftFragments = left.split("[|]");
String[] rightFragments = right.split("[|]");
if(leftFragments[0].compareTo(rightFragments[0]) == 0) {
return leftFragments[1].compareTo(rightFragments[1]);
} else {
return leftFragments[0].compareTo(rightFragments[0]);
}
}
});
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an ArrayList of type Vertex. Vertex is my own class which contains only one data member of String type. It has a member function getName() which returns the name of the Vertex.
I want to get the position in the ArrayList, if a particular string is given. I've written the below code to do it. But it always returns -1, which is the initial value. What is the problem with my code?
public int map(String vname)
{
int pos=-1;
for(int i=0;i<nodes.size();i++)
{
if(nodes.get(i).getName()==vname)
{
pos=i;
break;
}
}
return pos;
}
In the above code, nodes is an ArrayList of type Vertex.
Use String#equals: nodes.get(i).getName().equals(vname) instead. == in Java compares address of the
string.
== compares references not the content.
Use String#equals() if case is important otherwise use String#equalsIgnoreCase().
In Java == is for object equality, string1.equals(string2) is for string equality
You String comparison is wrong...
if(nodes.get(i).getName()==vname)
Should be
if(nodes.get(i).getName().equals(vname))
Replace it with
if(nodes.get(i).getName()==vname)
to
if(nodes.get(i).getName().equals(vname))
for comparing String with the case And for Comparing vname without case use
if(nodes.get(i).getName().equalsIgnoreCase(vname))
I'm having issues with a method I've written to search a class called Item. No matter what I search, it is returning null. I believe I'm having issues with variable scope:
public Item search(String itemSearch) {
Item search = null;
for(Item i : items){
if (i.getName() == itemSearch){
search = i;
}
}
return search;
}
The getName method returns the name attribute of the item. No matter what the Item search is always null, I'm guessing this is due to variable scope and it is not assigning in the for each loop? Why is this method always null?
Thank you
You can't use the == to compare the content of two strings in java. You need to use the .equals() method
Using the == will only compare the adress of the two strings, while equals will compare their values.
You are comparing strings using ==. You should instead use equals() method. E.G
i.getName().equals(itemSearch)
Also instead of looping the entire loop use return i in the if statement, instead of assigning i to search and then returning search.
I have a query and a resultset
I do this
while (rs.next())
{
String string = rs.getString(ColumnName);
if (String == "certainvalue")
{
//perform action
}else {
//do nothing
}
My problem is that the if condition doesn't seem to be working.... even though I know "certainvalue" is in the result set, it never evaluates to true, and it never performs the action---- I am confused as to why that is...
is it because i am using a while loop?? or is it because resultsets are just wierd,, ,what is going on???
Java can't compare strings with ==. What you have to do is use the equals method of the String.
if (string.equals("certainvalue")) {
perform action
}
It looks you're using Java. In that case, the == operator compares if the two objects are the same, not if they represent the same value.
Rewrite the test :
if ("certainvalue".equals(string)) { doStuff(); }
(You might consider "a".equals(b) to be equivalent to b.equals("a"), but the first form protects you from a NullPointerException if there is no value for the row in the database.)
String is an object. You can't do a comparison that way (you are trying to compare object reference).
If you are use java(aren't you?) try:
String string = rs.getString(columnName);
if (string.compareTo("anotherString") == 0){
}
You can use operator == just for primitive types (like int).