I'm working on my university homework and i have faced this problem. What i need to accomplish is when the user inputs a metallic element of either zinc, iron, aluminum and sodium i want the program to return true. When the element which is compared is true, the boolean still outputs false. Can you please identify the problem in this code?
class IonicCompound
public class IonicCompound {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a metallic element: ");
String element1 = input.nextLine();
System.out.println("Please enter a non-metallic element: ");
String element2 = input.nextLine();
Elements element = new Elements(element1, element2);
element.isMetal(element.first);
if (element.isMetal(element.first) == true) {
System.out.println("It's a metallic element ");
} else {
System.out.println("It's not a metallic element ");
}
}
}
class Elements
public class Elements {
public String first, second;
public Elements(String f, String s) {
first = f;
second = s;
}
public boolean isMetal(String ff) {
if (ff == "iron" || ff == "Iron" || ff == "aluminium" || ff == "Aluminium" || ff == "sodium" || ff == "Sodium" || ff == "zinc" || ff == "Zinc") {
return isMetal(ff) == true;
} else {
return false;
}
}
public String toString() {
String element = first + " " + second;
return element;
}