Click here to Skip to main content
16,018,797 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Here is my code -
Java
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");
		 }
		 if (!(rty > 2023 || rty < 1912 )) {
		 System.out.println("Enter your month of birth : (use only abc, ABC or ABC format)");
		 }
		 else {
			 System.out.println("");
		 }
		 Scanner rit = new Scanner(System.in);
		 String month = rit.nextLine();
		 switch (month)
		 {
		 case "January":
			 System.out.println("Garnet\nBold and alert\nWant to know more?");
			 break;
		 case "january":
			 System.out.println("Garnet\nBold and alert\nWant to know more?");
			 break;
		 case "JANUARY":
			 System.out.println("Garnet\nBold and alert\nWant to know more?");
			 break;
		 case "FEBRUARY":
			 System.out.println("Amethyst\nLucky and loyal\nWant to know more?");
			 break;
		 case "february":
			 System.out.println("Amethyst\nLucky and loyal\nWant to know more?");
			 break;
		 case "February":
			 System.out.println("Amethyst\nLucky and loyal\nWant to know more?");
			 break;
		 case "MARCH":
			 System.out.println("Aquamarine\nNaughty and genius\nWant to know more?");
			 break;
		 case "March":
			 System.out.println("Aquamarine\nNaughty and genius\nWant to know more?");
			 break;
		 case "march":
			 System.out.println("Aquamarine\nNaughty and genius\nWant to know more?");
			 break;
		 case "APRIL":
			 System.out.println("Diamond\nCaring and strong\nWant to know more?");
			 break;
		 case "april":
			 System.out.println("Diamond\nCaring and strong\nWant to know more?");
			 break;
		 case "April":
			 System.out.println("Diamond\nCaring and strong\nWant to know more?");
			 break;
		 case "MAY":
			 System.out.println("Emerald\nLoving and practical\nWant to know more?");
			 break;
		 case "May":
			 System.out.println("Emerald\nLoving and practical\nWant to know more?");
			 break;
		 case "may":
			 System.out.println("Emerald\nLoving and practical\nWant to know more?");
			 break;
		 case "JUNE":
			 System.out.println("Alexandrite\nRomantic and curious\nWant to know more?");
			 break;
		 case "june":
			 System.out.println("Alexandrite\nRomantic and curious\nWant to know more?");
			 break;
		 case "June":
		     System.out.println("Alexandrite\nRomantic and curious\nWant to know more?");
			 break;
		 case "JULY":
			 System.out.println("Ruby\nAdventurous and honest\nWant to know more?");
			 break;
		 case "July":
			 System.out.println("Ruby\nAdventurous and honest\nWant to know more?");
			 break;
		 case "july":
			 System.out.println("Ruby\nAdventurous and honest\nWant to know more?");
			 break;
		 case "AUGUST":
			 System.out.println("Peridot\nActive and hardworking\nWant to know more?");
			 break;
		 case "August":
			 System.out.println("Peridot\nActive and hardworking\nWant to know more?");
			 break;
		 case "august":
			 System.out.println("Peridot\nActive and hardworking\nWant to know more?");
			 break;
		 case "SEPTEMBER":
			 System.out.println("Sapphire\nSensitive and pretty\nWant to know more?");
		 case "September":
			 System.out.println("Sapphire\nSensitive and pretty\nWant to know more?");
			 break;
		 case "september":
			 System.out.println("Sapphire\nSensitive and pretty\nWant to know more?");
			 break;
		 case "OCTOBER":
			 System.out.println("Tourmaline\nStylish and friendly\nWant to know more?");
			 break;
		 case "October":
			 System.out.println("Tourmaline\nStylish and friendly\nWant to know more?");
			 break;
		 case "october":
			 System.out.println("Tourmaline\nStylish and friendly\nWant to know more?");
			 break;
		 case "NOVEMBER":
			 System.out.println("Citrine\nNice and creative\nWant to know more?");
			 break;
		 case "November":
			 System.out.println("Citrine\nNice and creative\nWant to know more?");
			 break;
		 case "november":
			 System.out.println("Citrine\nNice and creative\nWant to know more?");
			 break;
		 case "DECEMBER":
			 System.out.println("Zircon\nConfident and freedom loving\nWant to know more?");
			 break;
		 case "December":
			 System.out.println("Zircon\nConfident and freedom loving\nWant to know more?");
			 break;
		 case "december":
			 System.out.println("Zircon\nConfident and freedom loving\nWant to know more?");
			 break;
	     default:
	    	 System.out.println("Invalid month of birth");
		
		
		 }
	
		 Scanner tyi = new Scanner(System.in);
		String answer = tyi.nextLine();
		 if (answer.equals("yes"))		 {
			 System.out.println("ok");
		 }
		 else if (tyi.equals("no"))
		 {
			System.out.println("OK");
		 } else {
			 System.out.println("invalid");
		 }
		 
	     
	}
}


What I have tried:

I want that if any of the month matches, then only it should ask user input i.e tyi. Otherwise, if invalid month of birth is printed, it should not ask user input. How can we do that?
Also can I use (answer.equals("yes" , "Yes" , "YES")) or (answer.equals("yes" || "Yes" || "YES")). If not then how can I use this thrice to include all statements without using switch case.
Posted
Updated 23-Jul-23 6:01am
v2
Comments
Richard MacCutchan 23-Jul-23 3:48am    
If you went and studied the documentation like we keep suggesting, you would find the answer to that question quite easily.
Graeme_Grant 23-Jul-23 4:12am    
When I see that code dump it's like that intro scene in the Simpsons where Bart is repeatedly writing on the chalkboard... Over 750 episodes and he never learns...

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 -
Java
if (answer.equalsIgnoreCase("yes")) {
    //Some code to do something...
}


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 -
Java
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");
        }

        //Create a flag to keep track of valid month...
        boolean isValidMonth = false; 

        if (!(rty > 2023 || rty < 1912)) {
            System.out.println("Enter your month of birth : (use only abc, ABC or ABC format)");
            //Convert your input to uppercase for case-insensitive matching...
            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; //Set your flag to true if a valid month is found
                    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(); //Convert your input to lowercase for case-insensitive matching...

            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.
 
Share this answer
 
Comments
Rudra 2022 23-Jul-23 7:11am    
Thank you so much..
Andre Oosthuizen 23-Jul-23 7:20am    
You're welcome!
Rudra 2022 23-Jul-23 7:41am    
You really helped me in this. Thanks a lot..
You really don't listen when we tell you things, do you: How to continue this line?[^]

Use the String.toLowerCase method: Java String toLowerCase() Method[^]
 
Share this answer
 
Quote:
Java
(answer.equals("yes" || "Yes" || "YES"))

No, not this way!
but:
Java
(answer.equals("yes") || answer.equals("Yes") || answer.equals("YES"))
 
Share this answer
 
Comments
Rudra 2022 23-Jul-23 8:58am    
Thank you
...SMH...
If you're duplicating the same code over and over again, that is a major hint you're doing something wrong.

Why are you comparing to various versions of the same string when you can just covert the string to, say, all lowercase, then do a single comparison to that?
Scanner rit = new Scanner(System.in);
String month = rit.nextLine();
month = month.toLower();
switch (month)
{
case "january":
    System.out.println("Garnet\nBold and alert\nWant to know more?");
    break;
case "february":
    System.out.println("Amethyst\nLucky and loyal\nWant to know more?");
    break;
case "march":
    System.out.println("Aquamarine\nNaughty and genius\nWant to know more?");
    break;
case "april":
    System.out.println("Diamond\nCaring and strong\nWant to know more?");
    break;
case "may":
    System.out.println("Emerald\nLoving and practical\nWant to know more?");
    break;
case "june":
    System.out.println("Alexandrite\nRomantic and curious\nWant to know more?");
    break;
case "july":
    System.out.println("Ruby\nAdventurous and honest\nWant to know more?");
    break;
case "august":
    System.out.println("Peridot\nActive and hardworking\nWant to know more?");
    break;
case "september":
    System.out.println("Sapphire\nSensitive and pretty\nWant to know more?");
    break;
case "october":
    System.out.println("Tourmaline\nStylish and friendly\nWant to know more?");
    break;
case "november":
    System.out.println("Citrine\nNice and creative\nWant to know more?");
    break;
case "december":
    System.out.println("Zircon\nConfident and freedom loving\nWant to know more?");
    break;
default:
    System.out.println("Invalid month of birth");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900