Hey

Is there a way I can print out the line Im currently on


Example:

public class something
{

public void somefunction()
{
System.out.println("I am line number + " /*the number 6 would appear here*/);
System.out.println("I am line number + " /*the number 7 would appear here*/);

System.out.println("I am line number + " /*the number 9 would appear here*/);
}


}

Thank you

public static int getLineNumber() {    
   return Thread.currentThread().getStackTrace()[3].getLineNumber(); 
}

"The index will change based on the JVM version. I believe it changed from 1.4 to 1.5". Resource: http://bit.ly/xLDhVJ

I have changed the index to [3] instead of [2] but try either based on the version you are using until the line number of where function was called from is correct.

Hope this helps :)

commented: cool :) +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.