I'm currently making a project that finds percentage and so on on numbers. However, when I made the if statement to determine what processes to carry out based on the user input, it skips over to the final else statement even if the input matches the if statement. Here is my code.
public class Percentage {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter 'a' if you want to find a percentage of a given number.");
System.out.println("Enter 'b' if you want to find how much percent of a given number is a certain number.");
String choice = input.nextLine();
if (choice == "a"){
System.out.println("Enter the number.");
float numA = input.nextFloat();
System.out.println("Enter what percentage of the number you want to find.");
float perA = input.nextFloat();
float decA = perA * 0.01F;
float outA = numA * decA;
System.out.println(perA + "% of" + numA + "is: " + outA);
} else if (choice == "b"){
System.out.println("Enter the main number.");
float mainB = input.nextFloat();
System.out.println("Enter the number whose percentage of the main number you want to find.");
float minB = input.nextFloat();
float quoB = minB/mainB;
float perB = quoB * 100;
System.out.println(minB + "is" + perB + "% of " + mainB);
} else {
System.out.println("Invalid answer.");
}
input.close();
}
}
When I run the program and input either a or b, it goes straight to the else statement outputting "Invalid answer." What's wrong in my code?