Scanning a program for keywords

Must use recursion, scanner, set, and File IO

Page 1 of 1

14 Replies - 2810 Views - Last Post: 13 September 2010 - 06:58 AM Rate Topic: -----

#1 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Scanning a program for keywords

Posted 11 September 2010 - 08:31 PM

I am trying to write a program that scans a user specified program for programming keywords (public, class, private, static, etc.). I must use recursion, a set to store all keywords, and read from a file.

The program description is in the first few lines of code. I started it, but I really don't know how to process the user info from a command line. I also am not sure how to NOT include keywords that happen to be in comments or strings. Any help? I looked through several topics, but didn't see anything that really helped me. If you know of any please make me aware of them. Thanks.

/*
 * CSCI 2410
 * Meghan E. Hembree
 * Friday, September 3, 2010, 11:00:00am
 *
 * Description: (Counting the keywords in Java source code) Write a
 * program that reads a Java source-code file and reports the number
 * of keywords (including null, true, and false) in the file. Pass
 * the Java file name from the command line.
 *
 * (Hint: Create a set to store all the Java keywords.)
 *
 */

package exercise22_3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws FileNotFoundException{
       
        if(args.length != 1){
            System.out.println("Usage: java Test filename");
            System.exit(0);
	}
        //Command line.
        Scanner input = new Scanner(new File(args[0]));

        while (true){ //runs forever
	String s = input.next(); //reads one string
	System.out.println(s);
    }
    }

    public void countKeywords(){

        Scanner file = new Scanner(System.in) ;

        String comment = "//";
        int keywordCounter = 0;
        Set<String> set = new HashSet<String>();
        set.add("public");
        set.add("void");
        set.add("static");
        set.add("class");
        set.add("if");
        set.add("else");
        set.add("throws");
        set.add("null");
        set.add("true");
        set.add("false");
        set.add("import");
        set.add("new");
        set.add("int");
        set.add("boolean");
        set.add("long");
        set.add("double");
        set.add("return");


        if(file.hasNext()){
            if(file.next().equalsIgnoreCase(comment)){
                file.nextLine();
            }
            if(set.contains(file.next())){
                keywordCounter++;
            }

        }
        System.out.println("There are " + keywordCounter + " keywords in this"
                + "program");
        return;

	}

    }




Is This A Good Question/Topic? 0
  • +

Replies To: Scanning a program for keywords

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Scanning a program for keywords

Posted 11 September 2010 - 08:56 PM

If I were you I will change the declaration of the countKeywords method by:
public void countKeywords(String fileName)



So you pass args[0] as parameter and then within countKeywords read the file:
Scanner input = new Scanner(new File(fileName));


Then use a while loop for read each line:
while(input.hasNext()) 


Beside that you can use the startWith method to check whether or not a string start with comments:
if(file.next().startWith(comment))


Was This Post Helpful? 1
  • +
  • -

#3 tectonic.software  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 88
  • Joined: 12-February 10

Re: Scanning a program for keywords

Posted 11 September 2010 - 09:07 PM

First off, you never call countKeywords in your main method (I didnt know if this was intentional or not). Secondly, Scanner file = new Scanner(System.in) ;, is not reading from a File (Didnt know if this was intentional either). if(file.hasNext()) should be a while statement, Otherwise your only going to read one line then break the method. I personally would make a String object named line or sumn then have do all your equals and contains on that.

How the code should look:
 while(file.hasNext()){
	String line = file.nextLine().trim();
	if(line.startsWith(comment)){
		//comment
	} else {
		String[] possibleKeyWords = line.split(" ");//This checks for multiple keywords on one line.
		for(String possible : possibleKeyWords){
			if(set.contains(possible)){
				//found a keyword
			}
		}
	}
}



Sorry for my horrible formatting

edit by Dogstopper: Don't use horrible formatting...

This post has been edited by Dogstopper: 12 September 2010 - 06:13 AM

Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,587
  • Joined: 27-December 08

Re: Scanning a program for keywords

Posted 12 September 2010 - 10:10 AM

Some suggestions for dealing with comments.

Single line: Anything from // on should be ignored.

Multi-line: I'd flag a boolean here. When you come across a /* switch the boolean to true. As long as the boolean is true, ignore the input. Then when you come across */, invert the boolean again and continue looking at everything after the */.
Was This Post Helpful? 1
  • +
  • -

#5 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: Scanning a program for keywords

Posted 12 September 2010 - 03:49 PM

All of you guys, thanks so much! This really helped a lot! I think I can take it from here, but if I run into anything that I don't understand I'll repost my edited code. Thanks! Oh, can someone understand how the "pass commands or read files from the command line" means? Thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Scanning a program for keywords

Posted 12 September 2010 - 04:27 PM

it means that you pass it when you execute your program from the command line. Look at my blog post if you don't know how to do this. Basically, when you get to the stage when you can run the file (after compiling it and cd to the directory with the .class file in it) you would call
Java Exercise22_3 c:\whatever

whatever being the path to the file you want read
Was This Post Helpful? 1
  • +
  • -

#7 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: Scanning a program for keywords

Posted 12 September 2010 - 04:33 PM

Ok, so I edited this program and now I feel like it's something simple. When I run it it just print line 28 and then ends the program. How can I fix this and do you notice any other problems in my revised code?

/*
 * CSCI 2410
 * Meghan E. Hembree
 * Friday, September 3, 2010, 11:00:00am
 *
 * Description: (Counting the keywords in Java source code) Write a
 * program that reads a Java source-code file and reports the number
 * of keywords (including null, true, and false) in the file. Pass
 * the Java file name from the command line.
 *
 * (Hint: Create a set to store all the Java keywords.)
 *
 */

package exercise22_3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws FileNotFoundException{
       
        if(args.length != 1){
            System.out.println("Usage: java Test filename");
            System.exit(0);
	}
        //Command line.
        Scanner input = new Scanner(new File(args[0]));

        while (true){
	String s = input.next();
	System.out.println(s);
    }
    }

    public void countKeywords(String fileName) throws FileNotFoundException{

        Scanner file = new Scanner(new File(fileName)) ;

        String comment = "//";
        String blockCommentStart = "/*";
        String blockCommentEnd = "*/";
        int keywordCounter = 0;
        boolean isQuote = false;
        boolean isComment = false;
        Set<String> keywordList = new HashSet<String>();
        keywordList.add("public");
        keywordList.add("void");
        keywordList.add("static");
        keywordList.add("class");
        keywordList.add("if");
        keywordList.add("else");
        keywordList.add("throws");
        keywordList.add("null");
        keywordList.add("true");
        keywordList.add("false");
        keywordList.add("import");
        keywordList.add("new");
        keywordList.add("int");
        keywordList.add("boolean");
        keywordList.add("long");
        keywordList.add("double");
        keywordList.add("return");


        while(file.hasNext()){
            String line = file.nextLine().trim();

        if(line.startsWith(comment)){
            file.nextLine();
        }
        if(line.startsWith(blockCommentStart)){
            isComment = true;

            while(isComment == true){
                file.skip(line);
            }
            if(line.contains(blockCommentEnd)){
                isComment = false;
            }

        } else {
            //This checks for multiple keywords on one line.
            String[] possibleKeyWords = line.split(" ");

            for(String possible : possibleKeyWords){

                if(keywordList.contains(possible)){
                    keywordCounter++;
            }
        }
    }
}
        System.out.println("There are " + keywordCounter + " keywords in this"
                + "program");
        return;

	}

    }



Was This Post Helpful? 0
  • +
  • -

#8 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Scanning a program for keywords

Posted 12 September 2010 - 04:38 PM

It's doing exactly what it's supposed to, according to your instructions. You didn't pass an argument from the command line, so your if statement says, "hey, there wasn't an args[0] passed to main, so I'm gonna throw an error". You MUST run this program from the command line or else it won't work
Was This Post Helpful? 1
  • +
  • -

#9 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: Scanning a program for keywords

Posted 12 September 2010 - 04:42 PM

OH! Wow... okay... so now let me go look at your blog again.
Was This Post Helpful? 0
  • +
  • -

#10 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: Scanning a program for keywords

Posted 12 September 2010 - 05:02 PM

Okay, still having problems with command prompt. What type of file does command prompt run? I tried several different files from my classes, build, and source folders and none are working. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,587
  • Joined: 27-December 08

Re: Scanning a program for keywords

Posted 12 September 2010 - 05:05 PM

I'm not sure if your instructor is talking about using something like Scanner to read in from console input (in many cases it is command line), or about passing the Strings to search for when you run your program java ClassName comm1 comm2 comm3, etc. Scanner might be an easier option if it is permissible and you do not have command line set up to run Java programs.
Was This Post Helpful? 1
  • +
  • -

#12 tectonic.software  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 88
  • Joined: 12-February 10

Re: Scanning a program for keywords

Posted 12 September 2010 - 05:28 PM

In your code you specify that there must be a argument. Try running Command Prompt and running this command:

java Main (Your file name here)



And another thing, Your program never runs the method countKeywords() ;)
Was This Post Helpful? 1
  • +
  • -

#13 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: Scanning a program for keywords

Posted 12 September 2010 - 06:11 PM

View Postmacosxnerd101, on 12 September 2010 - 04:05 PM, said:

I'm not sure if your instructor is talking about using something like Scanner to read in from console input (in many cases it is command line), or about passing the Strings to search for when you run your program java ClassName comm1 comm2 comm3, etc. Scanner might be an easier option if it is permissible and you do not have command line set up to run Java programs.


I think he means both. He ran his program through command prompt AND specified the search string in command prompt. I have command prompt set up to run Java programs now. I'm stumped. Plus I can't test my program to see if it works because I can't run it. I don't know what to do now.
Was This Post Helpful? 0
  • +
  • -

#14 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: Scanning a program for keywords

Posted 12 September 2010 - 06:20 PM

View Posttectonic.software, on 12 September 2010 - 04:28 PM, said:

In your code you specify that there must be a argument. Try running Command Prompt and running this command:

java Main (Your file name here)



And another thing, Your program never runs the method countKeywords() ;)


Okay, very elementary question. How do I make my program run the method countKeywords()? I tried to call it from the main but it didn't work. It gave me an error saying "no arguments found".
Was This Post Helpful? 0
  • +
  • -

#15 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Scanning a program for keywords

Posted 13 September 2010 - 06:58 AM

no argument is found because you aren't passing it through the command line.

look at this:
 public static void main(String[] args)
ever wonder what the String[] args is for? Well, it's an array used to store arguments. If there is no argument used in the command line, the if statement ends the program. If there is more than one argument, the program takes the args[0] and tries to create a File to be read by your Scanner.

The command prompt will read .txt and .java files with no problem if it's done right. In your IDE, right click on the file in the hierarchy window and select properties. This will give you the location of your .java file. It should be in the source file, but we want the .class file stored in the bin folder. Open command prompt and cd to this file location. This is the directory to my bin folder for that class.

cd "c:\Users\YourUser\Documents\Java Programs\2410\bin"


now you just run the program:
java Exercise22_03 c:\Users\YourUser\Documents\Java Programs\2410\src\Exercise22_03.java
this will run the program and pass the .java file to the main() method
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1