DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  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

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links