Is It a Valid Identifier?
Read one line of text and decide whether it is a valid Java identifier.
A valid identifier: is non-empty; starts with a letter, _ or $; every character is a letter, digit, _ or $; and is not a Java reserved word (also excluding true, false, null).
Input: one line — the candidate (it may contain spaces, which make it invalid).
Output: Valid or Invalid.
totalMarks
Valid
2name
Invalid
- 1 <= line length <= 60
Hint 1
Check the first character, then every remaining character, against the rules.
Hint 2
Character.isLetter, Character.isLetterOrDigit, and a check for '_' / '$' cover the cases.
Hint 3
Finally, reject the word if RESERVED contains it.
First rule out the empty string and reserved words. Then verify the first character is a letter, underscore or dollar sign, and that every other character is a letter, digit, underscore or dollar sign. A space fails that test, so "my age" is Invalid.