can someone help me create this code and explain what these directions are askin
ID: 3680084 • Letter: C
Question
can someone help me create this code and explain what these directions are asking me to do? (Java syntax checker) Write a program that checks whether a user-input Java for-loop definition is valid. Use the simplified rules that follow. No optional spaces are allowed. The statement is composed by 4 fields, delimited as follows substring1 ’(’ substring2 ’;’ substring3 ’;’ substring4 ’)’ substring1 must be the string "for". substring2 must be the string "int" followed by a space, a string s (the variable name), the "=" sign, and an arbitrary string without spaces (i.e., it could be a number, a variable, etc.). substring3 must be a string that starts with the string s used in substring2, followed by an inequality symbol and an arbitrary string without spaces (i.e., it could be a number, a variable, etc.). substring4 must be a string that starts with the string s used in substring2, followed by "++" or "--". These are simplified rules. Feel free to enrich them to accept any valid Java for-loop statement
Explanation / Answer
public class JavaSyntaxChecker {
public static boolean forLoopChecker(String s){
int i=s.indexOf('(',0);
System.out.println("i"+i);
String s1=s.substring(0,i);
System.out.println("s1"+s1);
if(!(s1.equals("for"))){
System.out.println("syntax error in s1");
return false;
}
int j=i+1;
int i1=s.indexOf(';',j);
String s2=s.substring(i+1,i1);
System.out.println("s2"+s2);
if(!(s2.substring(0,2).equals("int"))){
System.out.println("syntax error in s2");
return false;
}
if(!(s2.substring(3,3).equals(" "))){
System.out.println("syntax error in s2 part 2");
return false;}
if(!(s2.substring(4,4).equals("s") && s2.substring(5,5).equals("="))){
System.out.println("syntax error in s2 part 3");
return false;
}
char c=s2.charAt(6);
if(!((c>='a' && c<='z')|| (c>='A' && c<= 'Z')|| (c>='0' && c<='9'))){
System.out.println("syntax error in s2 char");
return false;
}
int i2=s.indexOf(';',i1+1);
String s3=s.substring(i1+1,i2);
if(!(s3.substring(0,0).equals("s")))
{
System.out.println("syntax error in s3");
return false;
}
if(!(s3.substring(1,1).equals(">") || s3.substring(1,1).equals("<") ) )
{
if(s3.substring(1,2).equals("==")|| s3.substring(1,2).equals("!=")||s3.substring(1,2).equals(">=")||s3.substring(1,2).equals("<="))
{
char c1=s3.charAt(3);
if(!((c1>='a' && c1<='z')|| (c1>='A' && c1<= 'Z')|| (c1>='0' && c1<='9'))){
System.out.println("syntax error in s4");
return false;
}
}
}else{
char c1=s3.charAt(3);
if(!((c1>='a' && c1<='z')|| (c1>='A' && c1<= 'Z')|| (c1>='0' && c1<='9'))){
System.out.println("syntax error");
return false;
}
}
int a=i2-1;
String a1=s2.substring(4,4);
int i4=s.indexOf(')', a);
String s4=s.substring(j+1,i4);
if(!(s4.equals(a1+"++")|| s4.equals(a1+"--")))
{
System.out.println("Syntax error");
return false;
}
return true;
}
public static void main(String[] s)
{
forLoopChecker("for(int i=0;i<10;i++)");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.