Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Java syntax checker) Write a program that checks whether a user-input Java for-

ID: 3680655 • Letter: #

Question

(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 static void testForLoop() {
       String syntax = "for (int jj=0; jj!=7; jj++)";
       String str[] = new String[4];
       int index[] = new int[4];
       for (int i = 0; i < syntax.length(); i++) {
           if (syntax.charAt(i) == '(') {
               str[0] = syntax.substring(0, i).trim();
               index[0] = i + 1;
           }
           if (syntax.charAt(i) == ';' && str[1] == null) {
               str[1] = syntax.substring(index[0], i).trim();
               index[1] = i;
           } else if (syntax.charAt(i) == ';' && str[1] != null) {
               str[2] = syntax.substring(index[1] + 1, i).trim();
               index[2] = i;
           }
           if (syntax.charAt(i) == ')' && str[3] == null) {
               str[3] = syntax.substring(index[2] + 1, i).trim();
               index[3] = i;
           }
       }

       for (int i = 0; i < str.length; i++) {
           System.out.println(str[i]);
       }
       validate(str);
   }

   public static void validate(String values[]) {
       if (!(values[0] != null && "for".equals(values[0]))) {
           System.out.println("Invalid substring1");
           return;
       }
       if (values[1] != null) {
           String str[] = values[1].split(" ");
           if (str.length == 2 && str[0].trim().equals("int")) {
               String temp[] = str[1].split("=");
               if (temp.length == 2 && temp[0].toLowerCase().charAt(0) >= 'a' && temp[0].toLowerCase().charAt(0) <= 'z'
                       && temp[1].trim().length() > 0) {

               } else {
                   System.out.println("Invalid substring2");
                   return;
               }
           } else {
               System.out.println("Invalid substring2");
               return;
           }
       }
       else {
           System.out.println("Invalid substring2");
           return;
       }
       if(values[2] != null){
           String temp[] = values[2].split("!=");
           if(temp.length ==2 && temp[0].toLowerCase().charAt(0) >= 'a' && temp[0].toLowerCase().charAt(0) <= 'z'
                   && temp[1].trim().length() > 0){
              
           }else {
               System.out.println("Invalid substring3");
               return;
           }
       }else {
           System.out.println("Invalid substring3");
           return;
       }
      
       if(values[3] != null){
           String temp[] = values[3].split("/++");
           if(temp.length != 2){
               temp = values[3].split("/--");
               if(temp[0].trim().toLowerCase().charAt(0) >= 'a' && temp[0].trim().toLowerCase().charAt(0) <= 'z'){
                  
               }else {
                   System.out.println("Invalid substring4");
                   return;
               }
           }else if(temp[0].trim().toLowerCase().charAt(0) >= 'a' && temp[0].trim().toLowerCase().charAt(0) <= 'z'){
              
           }else {
               System.out.println("Invalid substring4");
               return;
           }
       }else {
           System.out.println("Invalid substring4");
           return;
       }
      
       System.out.println("Valid for loop.");
   }

Call first method from main method in Java

===============================

public static void validate(String values[]) {
      
       //for
       if (!(values[0] != null && "for".equals(values[0]))) {
           System.out.println("Invalid substring1");
           return;
       }
      
       //int jj=0
       if (values[1] != null) {
           String str[] = values[1].split(" ");
           //Here we are checking for "int" after that some characters should be present
           if (str.length == 2 && str[0].trim().equals("int")) {
               String temp[] = str[1].split("=");
               if (temp.length == 2 && temp[0].toLowerCase().charAt(0) >= 'a' && temp[0].toLowerCase().charAt(0) <= 'z'
                       && temp[1].trim().length() > 0) {

               } else {
                   System.out.println("Invalid substring2");
                   return;
               }
           } else {
               System.out.println("Invalid substring2");
               return;
           }
       } else {
           System.out.println("Invalid substring2");
           return;
       }
      
       //Here we are for != and some characters jj!=7
       if (values[2] != null) {
           String temp[] = values[2].split("!=");
           if (temp.length == 2 && temp[0].toLowerCase().charAt(0) >= 'a' && temp[0].toLowerCase().charAt(0) <= 'z'
                   && temp[1].trim().length() > 0) {

           } else {
               System.out.println("Invalid substring3");
               return;
           }
       } else {
           System.out.println("Invalid substring3");
           return;
       }

       //jj++
       if (values[3] != null) {
           String temp[] = values[3].split("/++");
           if (temp.length != 2) {
               temp = values[3].split("/--");
               if (temp[0].trim().toLowerCase().charAt(0) >= 'a' && temp[0].trim().toLowerCase().charAt(0) <= 'z') {

               } else {
                   System.out.println("Invalid substring4");
                   return;
               }
           } else if (temp[0].trim().toLowerCase().charAt(0) >= 'a' && temp[0].trim().toLowerCase().charAt(0) <= 'z') {

           } else {
               System.out.println("Invalid substring4");
               return;
           }
       } else {
           System.out.println("Invalid substring4");
           return;
       }

       System.out.println("Valid for loop.");
   }