-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentifierValidator.java
More file actions
46 lines (37 loc) · 1.42 KB
/
IdentifierValidator.java
File metadata and controls
46 lines (37 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
public class IdentifierValidator{
public boolean isValidIdentifier(String str){
if(str== null || str.isEmpty()){
return false;
}
if(!Character.isLetter(str.charAt(0)) && str.charAt(0) != '_'){
return false;
}
for(int i =0;i<str.length(); i++){
char ch= str.charAt(i);
if(!Character.isLetterOrDigit(ch) && ch != '_'){
return false;
}
}
return true;
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
IdentifierValidator validator = new IdentifierValidator();
System.out.println("Enter the value you want to check :- ");
String str= sc.nextLine().trim();
boolean answer= validator.isValidIdentifier(str);
if(answer){
System.out.println(str + " is a Identifier");
}
else{
System.out.println(str + " is not a Identifier");
System.out.println("RULES FOR MAKING AN IDENTIFIER :- ");
System.out.println("1) Start with a letter or underscore");
System.out.println("2) It should contain letter, number and underscore only");
System.out.println("3) It should not be Empty");
}
System.out.println();
System.out.println("Program Terminated");
}
}