Well, this works partially, for "yes" its recognizes, for "y" - not.
I tried different solutions with
but I gave up. There is something simple I don't know.
which basically means ... it works.
just take a look at your original code:
....
String rightAnswer = "Yes";
....
System.out.println("Would you like to draw another triangle? (Yes/No): ");
String answer = input.nextLine();
if
(rightAnswer.equalsIgnoreCase(answer))
...
you are checking here for equality of value (not case sensitive). you're not checking whether or not answer is a substring of rightanswer.
so ... now you'll need to be clear about when you want the if to run. on each substring from rightAnswer (y, ye, e, es, yes, s) or only on yes and y.
my suggestion for the first:
use the indexOf() method to see if the substring is in rightAnswer or not. off course, there is no 'indexOfIgnoreCase()' method, but if you want it to stay case insensitive, you can do the next:
set both rightAnswer and answer to lowercases, like this:
rightAnswer = rightAnswer.toLowerCase();
before checking to see if the substring is present.
in case you want the second scenario: just 'Y' or 'y'
well ... just check whether or not 'answer' has a length of 1. if this is the case, you might have entered 'y' or 'Y'.
so, again: if you wish to keep the case insesitivity, set both the String objects to lower cases, like I showed above. now check the equality of answer.charAt(0) and rightAnswer.charAt(0).
This should help you out on that point.
@arka.sharma
1. if you look very closely to the OP's objective, you see that it is a very basic problem, which is often given to people just starting to learn to work with Java. as the wise ones say, crawl, before you run. at this point, I assume it is more important for him to learn the basics on how to use conditions and such, than to master the entire world of Object Oriented programming. now, his teacher, or the author of the book/tutorial he's following obviously figured this out, and propably instructed him to write this in the main.
2. whether the user inputs a negative number or not, is not the issue here. yes, it would be easily solved by adding a simple if-statement, but just as point 1, his assignment may as well have said, write it as if the user can not enter negative numbers, and don't wory with handling this event if it occurs. so, don't look for problems that aren't there, it might confuse him even more, which is, I hope, not your intention.