Question:
isLetter method in Java?
anonymous
2009-11-05 13:59:54 UTC
I'm working on a method which accepts the ZIP Code. This ZIP Code has to have the first 3 characters being letters (eg. VCT5656). I'm doing the below method so as the user is prompted to re-enter the code if the above-mentioned condition isn't met.

// "pc" is the variable which the user uses to enter the ZIP Code
String pc_upper_case = pc.toUpperCase();
char c1 = pc_upper_case.charAt(0);
char c2 = pc_upper_case.charAt(1);
char c3 = pc_upper_case.charAt(2);

while ( (Character.isLetter(c1) == false) && (Character.isLetter(c2) == false) && (Character.isLetter(c3) == false) )
{
System.out.println ("Re-enter Post code:");
pc = Keyboard.readString();
}
postcode = pc;


(note: variables "postcode" and "pc" are already initialized at the start ;) )

This method is still not working as it is still accepting anything written. Can anyone help? Thanks.
Five answers:
Jon T
2009-11-05 14:11:47 UTC
It looks like you require all three characters to be non-letters. Change your && to || and this makes it so if even 1 of them is not a letter it will require them to re-input the postal code.



Another thing you might want to look into is regular expressions. You can use the pc.match([regular expression string]) to determine if your string matches a pattern like "AAA####"



Look at the link that I provided you to determine how you might build the regex. Just for a hint if you do "[a-zA-Z]{3}" it will look for 3 letters. You can add to this to add in the numbers afterward and also there are special escape characters that represent all letters, but I'll let you read the webpage to discover those yourself. It will help you to learn
David Karr
2009-11-05 14:12:02 UTC
First of all, you'd be better off using a regular expression, instead of comparing each character. Assuming your example is consistent, the expression would just be "[A-Z]{3}\d{4}" (3 uppercase chars followed by 4 digits).



In any case, if it's not working, you should be able to step through your method in a debugger with your sample data and determine where it's not working the way you expect. While in the debugger, you can inspect all of your variable values, and depending on the debugger, you can dynamically enter expressions to evaluate (like "Character.isLetter(c1)") and see what the result is.
deonejuan
2009-11-05 14:52:05 UTC
your code might still work... booleans



while(

Character.isLetter( c1 ) ||

Character.isLetter( c2 ) ||

Character.isLetter( c3 ) {

System.out.println( "Whine, moan, carry on, you dumb b***ch!);

}



Firstly, your logic with && I have fallen for myself. You say with that statement ALL THREE must be false. When maybe we're looking for just one bad egg.



2ndly, Character.isLetter() magically is 'true' or it is 'false' no need to assign it at all. isLetter() is a method CALL.



Good luck. There are more recetient ways of verifying zip codes, but that will come to you later.
anonymous
2016-12-13 13:48:26 UTC
Character.isletter
mata
2009-11-05 14:31:38 UTC
why should c1, c2 and c3 change, when you reassign pc within the while-loop? think it through...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...