Find identifiers in a java source file
<p>I have the following assignment:</p>

<p>Write a program that reads a Java source file and produces an index of all identifiers in the file. For each identifier, print all lines in which it occurs. Call in.useDelimiter("[^A-Za-z0-9_]+").  Then each call to next returns a string consisting only of letters, numbers, and underscores.</p>

<p>My plan was to create a sample .java file, and read it in line by line. I'm not sure what is meant by an identifier, or what class has the useDelimiter method mentioned. Can anyone get me off on the right track? Thanks. I would use the homework tag, but it says that it is being removed.</p>

<p>Here is what I have so far. I can get it to print out all of the identifiers in the file, but how can I find out what line number the identifier is on?</p>

<pre><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);
    }
}
</code></pre>
