nus-cs2103-AY2425S1 / forum

12 stars 0 forks source link

💡Duke Level-1: Allow case-insensitive comparison for user inputs using equalsIgnoreCase() #19

Open MAOXIONGKAI opened 1 month ago

MAOXIONGKAI commented 1 month ago

Hello guys, just sharing a tip over here in case if you are wondering how you can make your chatbot more flexible when handling user inputs as an enhancement for user experience. I know this is not compulsory requirement for the project, but it may be helpful if you are wondering about how to do it in Java.

In Java we know that we can use equals() method to compare the values of two String objects instead of their references, however, we can use equalsIgnoreCase() method if you not only want to compare the String Objects just by values but also want the comparison to be case-insensitive. This method can be useful if you want the chatbot to response to a certain command regardless of how exactly it is spelled.

For example, if you want the chatbot to say goodbye to the user and exit regardless of whether the user inputs "Bye", "BYE", "bye" or any other form. Instead of writing

if (userInput.equals("bye")) {
    // Your exit logic
}

you can write something like this

if (userInput.equalsIgnoreCase("bye")) {
    // Your exit logic
}

Hope that helps if you are seeking some improvements for your project, thank you.

damithc commented 1 month ago

Thanks for sharing this, @MAOXIONGKAI 💯