Hello,
I am having an issue with my if/then/else statement for one of my methods. The method is supposed to take the user input of a character between 'A' and 'Z' (or 'a' and
'z') and return the character to the variable c. If a character other than 'A' thru 'Z' is submitted, it needs to inform the user and loop back. I think my only issue with my code is with the parameter of the if statement.
Looking at the ASCII Table, A is 65, Z is 90, a is 97, and z is 122.
Based on how I would do an if statement for an int, this is what I come up with (which is not correct)
First of all, I would use char literals (like 'a') instead of numbers.
Second, you need a character that is between 'a' and 'z' OR between 'A' and 'Z'. Your code says something different.
The condition should be true if
c is equal or greater than 'a' and c is equal or less than 'z' OR c is equal or greater than 'A' and c is equal or less than 'Z'.
Now read your code (the if condition) out loud.
Also, your method getChar(char min, char max) takes two parameters but then ignores them.
That means c is a String. You should declare c as a char.
By the way, c should be a local variable inside a method where you need it, not a static variable.
The same is true for the rest of your static variables.
You should always declare variables in the minimal possible scope.
You said that in your comments. If c is a variable c for the getChar() method let it be declared in the getChar() method.
Thank you for your help on that. Changing my variable, c, to a char, and my methods from String to char solved those problems.
But now I have a new problem. Line #4 and Line #8.
I use c.charAt(0) to take the first character if a String typed in (since I am requesting a char), but with this I am now getting errors.
Line 4's error is: char cannot be dereferenced
Line 8's error is: incompatible types: String cannot be converted to char
I have them as static variables because I am using each of them in two methods, a method with no parameters, and a method with parameters. Same variable. Am I not correct in keeping them static?
The more fields you have in the class, the more opportunity there is for things to go wrong.
Consider passing the local variable as a parameter to the other method.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.