0

I am using javaparser to parse a java file , when I count the "If" statement it display the output as an incremental number such that Eg: if there are 3 "if" statements then it displays as [ 1 2 3 ]

I want to get only the total number of "IF" statements. eg:[ 3].

I don't want to get all the incrementing count This is my source code.

package org.javaparser.examples.chapter2;

public class VoidVisitorComplete {

private static final String FILE_PATH = "src/main/java/org/javaparser/samples/prime.java";

public static void main(String[] args) throws Exception {

    CompilationUnit cu = StaticJavaParser.parse(new FileInputStream(FILE_PATH));
     

      VoidVisitor<Void> methodNameVisitor = new IfStmtVisitor();
      methodNameVisitor.visit(cu, null);             
}


private static class IfStmtVisitor extends VoidVisitorAdapter<Void> {
        
    
    int i=0 ;
    @Override
    public void visit(IfStmt n, Void arg) {
        //visit a if statement, add 1
        
        i++;

            System.out.println( getNumber() );
                  
    }
            
    public int getNumber() {
          
        return i;
    }
       
}
 

}

  • Now you print it every time you visit an if. Just remove that code. Instead, decide *when* you want to print the result. Once for each method? Each class? Something else? – Joachim Sauer Sep 01 '21 at 16:39
  • i want to print it only once when the IfStmt class is called. If i remove the S.O.P where should i put it to print the final "i" – Mohamed Ifham Sep 02 '21 at 12:24
  • 1
    What exactly is your question? If you only want to print the final result print it after all "if"s were counted? – SirHawrk Sep 02 '21 at 13:31
  • @SirHawrk that is what i want. i cannot print it after all "ifs" counted. Is there any way to print ??. I cannot put the S.O.P outside the method visit. Its showing error saying "Syntax error, insert ")" to complete MethodDeclaration". – Mohamed Ifham Sep 02 '21 at 14:45
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 05 '21 at 00:42
  • when I count the "If" statement it display the output as an incremental order such that Eg: if there are 3 "if" statements then it displays as [ 1 2 3 ]. I want to get only the total number of "IF" statements. eg:[ 3]. – Mohamed Ifham Sep 05 '21 at 05:29

1 Answers1

1

You have to put the print statement after all the if statements have been counted.

package org.javaparser.examples.chapter2;

public class VoidVisitorComplete {
    private static final String FILE_PATH = "src/main/java/org/javaparser/samples/prime.java";

    public static void main(String[] args) throws Exception {
        CompilationUnit cu = StaticJavaParser.parse(new FileInputStream(FILE_PATH));
        VoidVisitor<Void> methodNameVisitor = new IfStmtVisitor();
        methodNameVisitor.visit(cu, null);
        System.out.println(methodNameVisitor.getNumber());            
    }
}

private static class IfStmtVisitor extends VoidVisitorAdapter<Void> {
    int i = 0;

    @Override
    public void visit(IfStmt n, Void arg) {
        //visit a if statement, add 1
        i++;
    }
        
    public int getNumber() {
        return i;
    }
}
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23