Results 1 to 6 of 6
  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default Find all identifiers in a java file

    I have an assignment to produce an index of all identifiers in a java source file. I am reading it in using a Scanner, and setting the delimiter for the scanner using a regex pattern [^A-Za-z0-9_]+" as a delimiter. When I loop over the scanner (using hasNext()) and call next() inside the loop, I get a printout of all the identifiers in the file like I want. The problem is how to find out what line each of those identifiers are on in the file. Here is what I have.

    Java Code:
        public static void main(String[] args) {
        System.out.println("Enter the path to your JAVA source file: ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String pathToFile = null;
        try {
            pathToFile = br.readLine();
            File f = new File(pathToFile);
            Scanner s = new Scanner(f);
            s.useDelimiter("[^A-Za-z0-9_]+");
            while (s.hasNext()) {                     
                String identifier = s.next();
                System.out.println(identifier);
            }
        } catch (IOException ioe) {
            System.out.println("Unable to locate file. Program will exit.\n"
                               + ioe.toString());
            System.exit(0);
        }
    }

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Find all identifiers in a java file

    "[^A-Za-z0-9_]+" = NOT A-Z or a-z or 0-9 or _ ... any space (or any other character '$' for example) causes it to stop current read.... why not just nextLine() and a counter?

  3. #3
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default Re: Find all identifiers in a java file

    Actually, its the opposite. It is printing out all the keywords, variable names, etc. So it's working correctly, I just don't know how to get the line number.

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Find all identifiers in a java file

    How is that the opposite? The delimiter you're using is any non-Alphanumeric or underscore.

    When '\n' is part of the delimiter, but not the only delimiter it's impossible to count them! you should read in a line at a time, then you can count each line! Then split the line on your regex.

  5. #5
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default Re: Find all identifiers in a java file

    Ok, so if I use that approach, the s.useDelimiter() method isn't needed, correct?

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Find all identifiers in a java file

    Right. Then use String.split(YOUR_REGULAR_EXPRESSION) to get an array of words for each line.

    Java Code:
        public static void main(String[] args) {
        	String str = "This is a String.";
        	String[] split = str.split("[^A-Za-z0-9_]+");
        	for(String s:split){
        		System.out.println(s);
        	}
    
        }
    Last edited by SJF; 10-28-2012 at 03:14 AM.

Similar Threads

  1. java file reader, jgrasp can't find the file
    By aramiky818 in forum New To Java
    Replies: 3
    Last Post: 04-22-2011, 03:06 AM
  2. cannot find the class file when running the java command
    By kulangotski in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 12:34 PM
  3. valid java identifiers
    By kulangotski in forum New To Java
    Replies: 7
    Last Post: 01-09-2011, 10:47 PM
  4. Replies: 1
    Last Post: 02-05-2010, 05:53 PM
  5. Replies: 0
    Last Post: 02-05-2010, 04:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •