Search method always null - java

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.

Related

Java Map with Set as Value. How to get all values for a given key?

I need your help. I have this method:
Set<Games> getGames(Player player) {
Map<User, Set<Games>> map = repository.getGamesMap();
Set<Games> result = new HashSet<>();
for (Map.Entry<Player, Set<Games>> entry : map.entrySet()) {
if (entry.getKey().equals(player)) {
Set<Games> games = entry.getValue();
result.addAll(games);
}
}
return result;
}
The outcome is that nothing is added to result Set. If I skip the if condition, everything is added (games of all players, not only of a given one).
If I try something like:
return newHashSet(repository.getGamesMap().get(user));
I get null pointer when testing, even though games are added. So I probably have to check the key for each entry and that's what I'm trying to do.
The problem is probably in the if statement, as you correctly pointed out.
It seems that you have not overridden .equals() method inside the Player class.
If you don't want to do that, you can change the if statement to something like:
if (entry.getKey().getName().equals(player.getName())) {
assuming that the Player class has a method getName() returning a String and that you want to compare two Player instances based on this string equality.
You can adapt this equality condition either inside the if statement (as above), or inside the .equals() method of the Player (also overriding the hashCode() method, as Andreas commented).
If you did override the equals() method, but it still doesn't work, try to debug this method or just print when it returns true and when false.

how to compare two string values from custom adapter in android?

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.

How to search in a custom built arraylist?

I have an arraylist built like this:
In class: Strings.java
ArrayList<MyQueue> strings = new ArrayList<MyQueue>();
strings.add (new MyQueue("paper", "clips", "eraser"));
strings.add (new MyQueue("paperplane", "numbers", "pineapple"));
In class: MyQueue.java
--Constructor with 3 string parameters--
--Getters/setters for three strings--
Now in the Strings.java class, I want to search my ArrayList "strings" to see if it has the string "paper"?
How could I do this efficiently?
You should try to override the equals method to compare the objects using the first attribute. And then call contains method to check for the object that has "paper" attribute. contains method uses equals internally so that's the reason you need to override equals.
iterate through strings to get each MyQueue and then iterate through the elements in each MyQueue to see if it has "paper"
for(MyQueue mq:strings){
if(mq.getString1().equals("paper") ||
mq.getString2().equals("paper") ||
mq.getString3().equals("paper") )
return true;
}
return false;
where getString#() is the method for getting each of the Strings in MyQueue

What does boolean do under these circumstances?

public static boolean containsAll(String[] strings, String test)
{
if (test == null || strings.length == 0) {
return false;
}
for (String str : strings)
if (!test.contains(str))
return false;
return true;
}
I have no idea what I can add to a boolean inside the parentheses but I have this line of function in my script with help from a fellow SOF member.
Thing is, how do I know what to write in a parenthesis to declare stuff? I don't know the rules from my memory and I don't have any source I can relate to. Basically I have no idea why there is an array and a string declared in the parentheses.
Basically,
1) Why is there a line declaring an array and a string inside the parentheses?
2) Where can I relate to as a source to get more information about classes like "boolean" and what I can do to change their functions? Basically I want a book-like website I can relate to whenever I don't know about something in java.
Why is there a line declaring an array and a string inside the parentheses?
So that you can use those parameters within the method , to achieve something .
From the method declaration it seems :
public static boolean containsAll(String[] strings, String test)
The method tries to search for a String passed as parameter test within an array passed as parameter strings and returns boolean true or false depending on whether the strings array contains the test String or not. Or probably , the method was named containsAll() to signify that it ascertain whether all the elements of strings array contains test string !
EDITED: The method checks if all the elements of the strings array contains the test String.
Where can I relate to as a source to get more information about classes like "boolean" and what I can do to change their functions
boolean is primitive , Boolean is a wrapper class. Your method returns boolean primitive.

Need to change ResultSet.getString(Value)---- conditionally

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

Categories

Resources