3

I want to store the line number of classes and function of java program written in a textarea. I was following this link posted by me Storing the line number of all the occurrence of particular character in javascript with the logic that the classes and the function starts with "{" but it will not work if loop or condition statement comes in a program.

Here is a textarea content:

public class Largest {

public static void main(String[] args) {
    double[] numArray = { 23.4, -34.5, 50.0, 33.5, 55.5, 43.7, 5.7, -66.5 };
    double largest = numArray[0];

    for (double num: numArray) {
        if(largest < num)
            largest = num;
    }

    System.out.format("Largest element = %.2f", largest);
    }
}

The output: Array content is... 1 3 4 7. I want the array output only 1,3.

Please suggest me approach of how to identify and find the line number of classes and function.

Ratnesh
  • 1,554
  • 3
  • 12
  • 28
  • 2
    You could work with something like a stack. Increment a counter by the occurance of `{` and decrement it by the occurance of `}`. Then you can be sure that if the counter goes from 0 to 1, you'll encounter the start and if it goes from 1 to 0, the end of a class. The same goes for methods but with the numbers 1 and 2, respectively 2 and 1 – Lino Jan 14 '19 at 11:52

0 Answers0