Created
July 9, 2012 11:20
-
-
Save douglas-vaz/3075893 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Identifier | |
{ | |
public static void main(String [] args)throws IOException | |
{ | |
String id; | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("Enter a string: "); | |
id = br.readLine(); | |
fa(id); | |
} | |
public static void fa(String in) | |
{ | |
int state = 0; | |
for(int i = 0; i<in.length(); i++) | |
{ | |
char foo = in.charAt(i); | |
System.out.println("State: "+state); | |
if(state == 0) | |
{ | |
if(foo == '_' || (foo >= 'A' && foo <= 'Z') || (foo >= 'a' && foo <= 'z')) | |
state = 1; | |
else | |
break; | |
} | |
else if(state == 1) | |
{ | |
if(foo == '_' || (foo >= 'A' && foo <= 'Z') || (foo >= 'a' && foo <= 'z') || (foo >= '0' && foo <= '9')) | |
state = 1; | |
else{ | |
System.out.println("Not a valid string"); | |
state = -1; | |
break; | |
} | |
} | |
} | |
if(state == 0) | |
System.out.println("Not an identifier"); | |
else if(state == 1) | |
System.out.println("Identifier!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment