Re: Detecting Capitalization
You can test a char against 'a' for example (char > 'a'). chars can be treated as int in java
This kind of code: (char > 97) is poor because you have to look up the value of 97 to see what char it is.
Using a literal like 'Z' shows exactly what you are testing.
Re: Detecting Capitalization
Thanks for the reply. However, that isn't what I am looking for. For example, if someone entered:
Daniel
I want to say: if(firstletter is capital)
so I started saying
if(word.charAt(0) > ???)
I don't know what the word is yet so I can't actually write the specific character.
Would this work?
if(word.charAt(0) > 'z')
Would ^^^^ that mean that the word's first index be a capital letter?
Re: Detecting Capitalization
You should write a small program and try some different values to see what the results are.
Quote:
Would ^^^^ that mean that the word's first index be a capital letter?
Do you know whether 'a' is > or < 'A'?
Try this: System.out.println("'a' > 'A' = " + ('a' > 'A'));
Re: Detecting Capitalization
I got:
'a' > 'A' = true
so I should write:
if(word.charAt(0) > 'z')
then its a capital, right?
Re: Detecting Capitalization
Quote:
so I should write:
if(word.charAt(0) > 'z')
then its a capital, right?
Write a small test program, try different values and see what you get.
Re: Detecting Capitalization
Thank you for your help! I found out that this is the order:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx yz
Therefore if(word.charAt(0) < 'a')
we know it is capital :)!
(Just for reference in case others stumble upon this thread)
Re: Detecting Capitalization
Quote:
we know it is capital :)!
Sometimes yes, sometimes no.
There are a lot of other character values that are less than 'a'.
The best test would be for the letter to be in the range from 'A' to 'Z'
Re: Detecting Capitalization
Ok so how about
if(word.charAt(0) >= 'A' && word.charAt(0) <= 'Z')
:-)
Re: Detecting Capitalization
Re: Detecting Capitalization
What about something like:
Code:
public static boolean isCapitalized (String word) {
return Character.isUpperCase(word.charAt(0));
}