Write a program in java to check string is identifier or not | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a program in java to check string is identifier or not

Help.....

13th Feb 2019, 6:30 AM
Mohammad Umair
Mohammad Umair - avatar
5 Answers
+ 5
I like the fact that you like your own question and your own answer so much that you upvoted it and gave yourself the best answer, but that's not what the Q&A section is for. Please use the Q&A section only if you actually have a question or want to answer other people's (!) questions. https://www.sololearn.com/discuss/1316935/?ref=app
13th Feb 2019, 6:49 AM
Anna
Anna - avatar
+ 5
The problem lies within the `if` block, here's a fixed version of the `if` block with a little comment to explain the problem. Hope it helps. if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_') || (c == '
#x27;)) { // 1st character is good, increment <count> count++; // for(int i=1;i <= l;i++) // i <= l : index out of bounds for(int i = 1; i < l; i++) { // check variable <c>, not <l> c = s.charAt(i); // if((l >= 'a' && l <= 'z') || (l >= 'A' && l <= 'Z') || (l >= '0' && l <= '9') || (l == '_') || (l == '
#x27;)) if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_') || (c == '
#x27;)) { count++; } } if(count == s.length()) { flag=false; } } else { flag=true; }
14th Feb 2019, 9:32 AM
Ipang
+ 4
Hi Mohammad Umair It would help to save this code in Code Playground, then post a link here for others to review. Also, since you've marked your question as answered, you may not get many replies. I recommend removing the green checkmark next to your answer so others know this question is still unresolved.
13th Feb 2019, 8:37 AM
David Carroll
David Carroll - avatar
0
import java.util.*; class identi { public static void main(String arg[]) { System.out.print("Enter a character:"); Scanner sc=new Scanner(System.in); String s; int count=0; boolean flag = false; s=sc.nextLine(); int l=s.length(); char c=s.charAt(0); if( (c>='a'&& c<='z')||(c>='A'&& c<='Z')||(c=='_')||(c=='
#x27;)) { for(int i=1;i<=l;i++) { if((l>='a'&& l<='z')||(l>='A' && l<='Z')||(l>='0'&& l<='9')||(l=='_')||(l=='
#x27;)) { count++; } } if(count==s.length()) { flag=false; } } else { flag=true; } if(flag==false) System.out.println("It is a valid identifier"); else System.out.println("It is not a valid identifier"); } }
13th Feb 2019, 6:30 AM
Mohammad Umair
Mohammad Umair - avatar
0
my code not running right
13th Feb 2019, 7:05 AM
Mohammad Umair
Mohammad Umair - avatar