I am making a side project in Java where a Mobile will store a list (ArrayList) of apps and separate UI class to handle all input/ output and business related logic. The problem is in following block of Mobile class:
public App searchApp(String name)
{
System.out.println(list);
for(App app: list) {
String appName = app.getName();
if(appName == name)
{
return app;
}
}
throw new IllegalStateException();
}
This function let person search for the app in the list. And here's how the search input is being taken from user:
public String inputSearch()
{
System.out.println("Enter app name to search: ");
return sc.nextLine().trim();
}
And here's how the UI class is displaying result:
public void choices(int choice)
{
switch(choice)
{
case 1:
String[] input = inputDetails();
mobile.addApp(input[0], input[1]);
break;
case 2:
String s = inputSearch();
App found = mobile.searchApp(s);
System.out.println("App found in search:");
System.out.println(found.toString());
break;
and so on...
The problem is in searchApp.
I am unable to figure out why if condition is not working even though both appName and name can be seen as equal upon printing (also have used trim() on both string types but still no success).