-5

i want to count the total numer of variables from text file in java for this purpose we use this code

try {
    BufferedReader reader = new BufferedReader(new FileReader(fn));
    String line = reader.readLine();

    while(line !=null)
    {
        Scanner fs = new Scanner(reader);
        while(fs.hasNext())
        {
            String s = fs.next();

            if( s.startsWith("int")) {
                s1 = ";" ;

                while(!(s1.equals(s2))){        
                    Scanner fd = new Scanner(reader);

                    while(fd.hasNext()){
                        c = fd.next();

                        if(c.contains(","))
                            cint++;
                        else
                            cint++;

                        if(c.startsWith(";"))
                            break;                                              
                    }

                    s2 = c ;           
                }                   
            }         

            if(s.startsWith("short")) {
                cshort++;
            }           
            if(s.startsWith("byte")) {
                cbyte++;
            }           
            if(s.startsWith("long")) {
                clong++;
            }           
            if(s.startsWith("float")) {
                cfloat++;
            }
            if(s.startsWith("boolean")) {
                cboolean++;
            }           
            if(s.startsWith("double")) {
                cdouble++;
            }
            if(s.startsWith("char")) {
                cchar++;
            }
            if(s.startsWith("abstract")) {
                cabstract++;
            }
            if(s.startsWith("continue")) {
                ccontinue++;
            }
            if(s.startsWith("switch")) {
                cswitch++;
            }
            if(s.startsWith("assert")) {
                cassert++;
            }
            if(s.startsWith("default")) {
                cdefault++;
            }
            if(s.startsWith("goto")) {
                cgoto++;
            }
            if(s.startsWith("package")) {
                cpackage++;
            }
            if(s.startsWith("synchronized")) {
                csync++;
            }
            if(s.startsWith("do")) {
                cdo++;
            }
            if(s.startsWith("if")) {
                cif++;
            }
            if(s.startsWith("private")) {
                cprivate++;
            }
            if(s.startsWith("this")) {
                cthis++;
            }
            if(s.startsWith("break")) {
                cbreak++;
            }
            if(s.startsWith("implements")) {
                cimplements++;
            }
            if(s.startsWith("protected")) {
                cprotected++;
            }
            if(s.startsWith("catch")) {
                ccatch++;
            }
            if(s.startsWith("extends")) {
                cextends++;
            }
            if(s.startsWith("try")) {
                ctry++;
            }
            if(s.startsWith("final")) {
                cfinal++;
            }
            if(s.startsWith("interface")) {
                cinterface++;
            }
            if(s.startsWith("static")) {
                cstatic++;
            }
            if(s.startsWith("void")) {
                cvoid++;
            }
            if(s.startsWith("instanceof")) {
                cinstanceof++;
            }
            if(s.startsWith("class")) {
                cclass++;
            }
            if(s.startsWith("finally")) {
                cfinally++;
            }
            if(s.startsWith("strictfp")) {
                cstrictfp++;
            }
            if(s.startsWith("volatile")) {
                cvolatile++;
            }
            if(s.startsWith("const")) {
                cconst++;
            }
            if(s.startsWith("native")) {
                cnative++;
            }
            if(s.startsWith("super")) {
                csuper++;
            }
            if(s.startsWith("while")) {
                cwhile++;
            }
            if(s.startsWith("for")) {
                cfor++;
            }
        }

        line = reader.readLine();                       
    }               
} catch (Exception ex) {
    System.out.println(ex.getMessage());                       
}

insert();

The problem is this it gives wrong number of integer variable please can help me any on for this

krillgar
  • 12,596
  • 6
  • 50
  • 86

2 Answers2

0

Try adding some else if's instead of a million ifs. If a variable starts with one thing its not going to start with another. For example:

        if(s.startsWith("short")) {
            cshort++;
        }           
        else if(s.startsWith("byte")) {
            cbyte++;
        }  

That will also cut down on the compile time for your program. If you have access to the file you could make each variable on a separate line to make it easier to debug and read in.

Kooky_Lukey
  • 137
  • 1
  • 10
0

Lets just look at the code that attempts to count int variables:

        if( s.startsWith("int")) {
            s1 = ";" ;

            while(!(s1.equals(s2))){        
                Scanner fd = new Scanner(reader);

                while(fd.hasNext()){
                    c = fd.next();

                    if(c.contains(","))
                        cint++;
                    else
                        cint++;

                    if(c.startsWith(";"))
                        break;                                              
                }

                s2 = c ;           
            }                   
        }

Why is that going to give incorrect counts?

  1. You are treating every word that starts with "int" as an int keyword. But it isn't. What about internal or international ...

  2. What about int in comments? Comments should be ignored.

  3. What about int[] myarray;? That's not int variable. It is a int[] variable.

  4. What about return (int) someVariable; ? That is a typecast, not a declaration.

  5. What about public int someMethod() { ... } ? The return type is not a variable.

  6. What about public void method(int a, char b) { ... } ? That's declaring an int variable ... but your code will (I think) incorrectly count it as two int variables.

And so on.


Your approach would be best described as crude one-pass pattern matching, implemented directly as code. Basically, this approach to "analysing" source code is doomed to fail.

What you really need to do is to parse the Java source code properly using a parser that recognizes the Java grammar. You could:

  • write your own Java parser, or

  • make use of an existing Java parser, or

  • look for an existing Java grammar that is suitable for input to ANTLR or JavaCC or some other Java parser generator system (PGS).

Once you've done that, you could either walk an AST data structure emitted by the parser, or embed your "variable counting" code into the grammar in the PGS input file.


There is another approach (from the linked Q&A). Compile the source file, use Class.forName() to load it, and then use reflection to find Field objects for the static and instance variables. At a pinch, you could even count the parameters in the method signatures via the Method objects. But local variables are not exposed by reflection ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216