I ran in to a small problem while working on my assignment. Basically I’m making a small program that asks the user for 3 letters and compares them to 3 letters that are coded in. Then the program is supposed to compare the 3 letters and if they’re the same then print true. Thus far I’ve been able to make it without problems using compareTo, but now the tricky part is that I need to add a “tolerance” to the program (which I have) but the tolerance is supposed to loop back from Z to A. So if tolerance is 3 and the user inputs X Y Z (when it really is A B C) it should still print out true.
NOTE: the Tolerance will not go above 3. ALSO, We shouldn’t use Arrays.
Any ideas how I can accomplish this? If it’s complicated to understand what I’m asking please post and I’ll try to clarify it 🙂
Thanks
EDIT: basically this is the code that compares the tolerances
if ((a1.compareTo(d1) <= tolerance) && (a1.compareTo(d1) >= negTolerance)
&& (b1.compareTo(e1) <= tolerance) && (b1.compareTo(e1) >= negTolerance)
&& (c1.compareTo(f1) <= tolerance) && (c1.compareTo(f1) >= negTolerance))
{
open = true;
} else open = false;
where a1 – c1 are pre inputed characters and d1-f1 are user entered. tolerance is also user entered as an integer between 1 and 3
Since it’s a homework assignment, I won’t give you the full answer, but I’ll give you a hint. You want to look at the character codes (ASCII) for the letters. This will let you solve the tolerance problem. You might also have to do some magic with
%(modulus) to handle the looping back of Z to A.EDIT
If you cannot use the ASCII values, the return value of
compareTowill help you, but keep in mind that that comparingAtoZandZtoAwill give you -25 and 25 respectively. This is where the%operator will help you.