Created
August 9, 2020 12:54
-
-
Save theahmadzai/782a04e0332d1db0f0626537b4759dfa 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
package computergraphics; | |
import java.util.ArrayList; | |
public class Test { | |
public static void main(String[] args) { | |
validate("int dd,b, ZA,A, A, ddd,ac,a;"); | |
} | |
public static void validate(String statements) { | |
ArrayList<String> vn = new ArrayList<String>(); | |
String[] tokens = statements.trim().split(","); | |
if(!tokens[0].contains(" ")) { | |
System.out.println("Missing space in datatype"); | |
return; | |
} | |
String[] dv = tokens[0].split(" "); | |
tokens[0] = dv[1].trim(); | |
String[] last = tokens[tokens.length - 1].trim().split(";"); | |
if(last.length > 1 || last.length < 1) { | |
System.out.println("Invalid semicolon"); | |
return; | |
} | |
tokens[tokens.length-1] = last[0]; | |
for(String token : tokens) { | |
for(char l : token.trim().toCharArray()) { | |
if((l < 65 && l > 90) || (l < 97 && l > 122)) { | |
System.out.println("Invalid variable name"); | |
return; | |
} | |
} | |
if(vn.contains(token.trim())) { | |
System.out.println("Duplicate variable name"); | |
return; | |
} | |
vn.add(token.trim()); | |
} | |
System.out.println("Valid declarations!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment