Page 200 - Introduction to Programming with Java: A Problem Solving Approach
P. 200
166 Chapter 5 Using Pre-Built Methods
/***************************************************************
*
IdentifierChecker.java
Dean & Dean
Check a user entry to see if it's a legal identifier.
***************************************************************/
*
*
*
import java.util.Scanner;
public class IdentifierChecker
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String line;
char ch;
// user entry
boolean legal = true; // Is entered line a legal identifier?
System.out.println("This program checks the validity of a" +
" proposed Java identifier.");
System.out.print("Enter a proposed identifier: ");
line = stdIn.nextLine();
ch = line.charAt(0);
(!(Character.AispLeattgero(ch)P|D|Fch legal = false;
ch = line.charAt(i);
E==n'h$'a|n| che=r= for (int i=1; i<line.length() && legal; i++)
if
{
'_'))
}
// end class IdentifierChecker
}
// end main
}
{
}
}
else
{
}
if (!(Character.isLetterOrDigit(ch) || ch == '$' || ch == '_'))
{
}
legal = false;
if (legal)
{
System.out.println(
"Congratulations, " + line + " is a legal Java identifier.");
System.out.println(
"Sorry, " + line + " is not a legal Java identifier.");
Character
method calls
Figure 5.7 IdentifierChecker program