Cyclomatic complexity


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Cyclomatic complexity

  1. #1
    Join Date
    Oct 2004
    Location
    glasgow
    Posts
    8

    Unhappy Cyclomatic complexity

    Hi I have been given an asignment to read a file and determine the cyclomatic complexity I have tried a few variations but cant seem to get the program to count the 'if','while' etc it always gives 1 as result
    does anyone know how to do this
    I would really appreciate any help below is 1 of the codes I have tried

    import java.io.*;
    import java.util.*;

    public class complexity {
    static BufferedReader keyboard=new
    BufferedReader(new InputStreamReader(System.in));
    static PrintWriter screen = new PrintWriter(System.out, true);
    public static void main(String[] args) throws IOException {

    String[] keywords = {"if","while","switch","for"};
    int complexity = 1;
    String fileName;


    screen.println();
    screen.println("Enter the Java file to be read: ");

    fileName = new String(keyboard.readLine());




    FileInputStream fs = new FileInputStream(fileName);
    InputStreamReader r = new InputStreamReader(fs);
    StreamTokenizer parser = new StreamTokenizer(r);

    while ( parser.nextToken() != StreamTokenizer.TT_EOF )
    {
    if ( parser.ttype == StreamTokenizer.TT_WORD)
    {

    for(int i=0; i<keywords.length; i++)
    {

    if ((parser.sval).equals(keywords))
    {
    complexity ++;
    }
    }
    }
    }
    screen.println("complexity = : "+complexity);
    }
    }
    best wishes charliebhoy

  2. #2
    Join Date
    Oct 2004
    Location
    New Zealand
    Posts
    1
    You've forgotten to index the array in the test at the heart of your program:

    if ((parser.sval).equals(keywords)) {...}
    should be:
    if ((parser.sval).equals(keywords[i])) {...}

    Andrew

  3. #3
    Join Date
    Oct 2004
    Location
    glasgow
    Posts
    8
    Thanks Andrew,
    I am relatively new to java so your help is invaluable
    Cheers m8
    best wishes charliebhoy

  4. #4
    Join Date
    Oct 2004
    Posts
    25
    java.io.StreamTokenizer may be good for you


    floaty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center