No, you cannot use your 'equals to' method, you can however use the 'equalsIgnoreCase()' method to compare a single string with multiple values -
Java String equalsIgnoreCase() Method[
^].
This will shorten your code substantially as you do not have to write a case for each variant of a month -
if (answer.equalsIgnoreCase("yes")) {
}
As for your code, before we search for a month, we convert it to Uppercase' (as used in our switch case) to make sure that all of the user's input will be matched correctly with the uppercase month strings in the switch statement. The correct code should then look like -
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your year of birth:");
int rty = sc.nextInt();
int p = (2023 - rty);
if (rty < 1912) {
System.out.println("Invalid year of birth. A person cannot be more than 111 years as dictated by my master Rudra");
} else if (rty > 2023) {
System.out.println("Invalid year of birth. Age cannot be in minus as dictated by my master Rudra");
} else {
System.out.println("Your age is : " + p + " " + "years");
}
boolean isValidMonth = false;
if (!(rty > 2023 || rty < 1912)) {
System.out.println("Enter your month of birth : (use only abc, ABC or ABC format)");
String month = sc.next().toUpperCase();
switch (month) {
case "JANUARY":
case "FEBRUARY":
case "MARCH":
case "APRIL":
case "MAY":
case "JUNE":
case "JULY":
case "AUGUST":
case "SEPTEMBER":
case "OCTOBER":
case "NOVEMBER":
case "DECEMBER":
isValidMonth = true;
break;
default:
System.out.println("Invalid month of birth");
}
}
if (isValidMonth) {
Scanner tyi = new Scanner(System.in);
System.out.println("Do you want to know more? (yes/no)");
String answer = tyi.nextLine().toLowerCase();
if (answer.equals("yes")) {
System.out.println("ok");
} else if (answer.equals("no")) {
System.out.println("OK");
} else {
System.out.println("Invalid input");
}
}
}
}
Lastly, you should really follow the comments/advice other people post for you if you want to keep on getting proper answers to your questions. First try and solve the puzzle yourself with the information given by others, then if you are really stuck, ask a question AROUND the actual problem and not just by dumping a bunch of code and expect us to search for a possible error.