473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to get the line number in the code?

Is there a way to obtain the line number in my code? For example:
1 public class MyClass {
2 int a = 5;
3 System.out.println("Integer variable a was set on line number: " + x);
4 etc.
Where x is equal to '2'.

How can I obtain 'x' ?

Thanks.
Jul 17 '05 #1
7 25935
You cannot do that in Java. The only way to get line number is by
creating a Throwable object and then extracting the stack trace. You
cannot find the line number at compile time.

Regards,
KB

Charles wrote:
Is there a way to obtain the line number in my code? For example:
1 public class MyClass {
2 int a = 5;
3 System.out.println("Integer variable a was set on line number: " + x);
4 etc.
Where x is equal to '2'.

How can I obtain 'x' ?

Thanks.


--
------------------------------------------------------------------------
This email is certified to be Spam free by Spam Marshall

You don't pay to get spam, why pay to clean it?
Visit http://www.spammarshall.com to create an account for free
<http://www.spammarshall.com>

------------------------------------------------------------------------
Jul 17 '05 #2
Kathy Benson <fo***********@yahoo.com> wrote in message news:<3F************@yahoo.com>...
You cannot do that in Java. The only way to get line number is by
creating a Throwable object and then extracting the stack trace. You
cannot find the line number at compile time.

Regards,
KB

Charles wrote:
Is there a way to obtain the line number in my code? For example:
1 public class MyClass {
2 int a = 5;
3 System.out.println("Integer variable a was set on line number: " + x);
4 etc.
Where x is equal to '2'.

How can I obtain 'x' ?

Thanks.


--
------------------------------------------------------------------------
This email is certified to be Spam free by Spam Marshall

You don't pay to get spam, why pay to clean it?
Visit http://www.spammarshall.com to create an account for free
<http://www.spammarshall.com>

------------------------------------------------------------------------
--


You can use java.io.LineNumberReader of LineNumberInputStream. Both
class has getLineNumber() method.
Jul 17 '05 #3
On 2 Oct 2003 20:32:13 -0700, HG******@nifty.ne.jp (hiwa) wrote:
>Is there a way to obtain the line number in my code? For example:
>1 public class MyClass {
>2 int a = 5;
>3 System.out.println("Integer variable a was set on line number: " + x);
>4 etc.
>Where x is equal to '2'.
>
>How can I obtain 'x' ?


You can use java.io.LineNumberReader of LineNumberInputStream. Both
class has getLineNumber() method.


Actually, the original poster wanted to do this at runtime. I suppose
you could do some fiddling by reading the .java file into
LineNumberReader, and locating the code you were executing.
Jul 17 '05 #4
Correct, I wish to be able to do this at runtime. I was hoping for a quick
and easy way to do this with minimum overhead.

"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:ik********************************@4ax.com...
On 2 Oct 2003 20:32:13 -0700, HG******@nifty.ne.jp (hiwa) wrote:
>Is there a way to obtain the line number in my code? For example:
>1 public class MyClass {
>2 int a = 5;
>3 System.out.println("Integer variable a was set on line number: " + x); >4 etc.
>Where x is equal to '2'.
>
>How can I obtain 'x' ?


You can use java.io.LineNumberReader of LineNumberInputStream. Both
class has getLineNumber() method.


Actually, the original poster wanted to do this at runtime. I suppose
you could do some fiddling by reading the .java file into
LineNumberReader, and locating the code you were executing.

Jul 17 '05 #5
On Fri, 3 Oct 2003 11:34:01 -0400, "Charles" <cs*@rogers.com> wrote:
Correct, I wish to be able to do this at runtime. I was hoping for a quick
and easy way to do this with minimum overhead.
Then the answer is no.
"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:ik********************************@4ax.com.. .
On 2 Oct 2003 20:32:13 -0700, HG******@nifty.ne.jp (hiwa) wrote:
>> >Is there a way to obtain the line number in my code? For example:
>> >1 public class MyClass {
>> >2 int a = 5;
>> >3 System.out.println("Integer variable a was set on line number: "+ x); >> >4 etc.
>> >Where x is equal to '2'.
>> >
>> >How can I obtain 'x' ?
>
>You can use java.io.LineNumberReader of LineNumberInputStream. Both
>class has getLineNumber() method.


Actually, the original poster wanted to do this at runtime. I suppose
you could do some fiddling by reading the .java file into
LineNumberReader, and locating the code you were executing.


Jul 17 '05 #6
Charles wrote:
Correct, I wish to be able to do this at runtime. I was hoping for a quick
and easy way to do this with minimum overhead.


[Java purists look away.]
The easiest way would be to use the C precompiler. Or you could even
write your own precompiler (perhaps even in Java, to satisfy the purists).

Ray

Jul 17 '05 #7
Charles wrote:[color=blue]
> Correct, I wish to be able to do this at runtime. I was hoping for a quick
> and easy way to do this with minimum overhead.
>[/color]

[Java purists look away.]
The easiest way would be to use the C precompiler. Or you could even
write your own precompiler (perhaps even in Java, to satisfy the purists).

Ray
------------------
This is the answer for jdk 1.4


String linenumber = new Exception().getStackTrace()[0].getLineNumber();
System.out.println("The line number is " + linenumber);
Mar 31 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
5
by: Mark Kamoski | last post by:
Hi Everyone-- How can one get the line number of where an error was thrown and/or caught? For example, note the following, for use at any given point in a piece of code: ....to get the...
10
by: Al | last post by:
Hi, I am seeking to obtain the line number of an error once deployed with out inserting line numbers in the method. The IDE inserts line numbers but this function does not appear to be a...
3
by: Rotsey | last post by:
Hi, I am trying to write some error handling code to log the error. But getting the line number of the error is not simple. Is it because the app is set to "Release" and does not have debug...
4
by: Jeff Jarrell | last post by:
I have a block of code that during development is prone to casting errors. It is mostly a DataReader type thing. It looks something like this. _prtPNID = myDLReader.GetString("prtPNID")...
5
by: IdleBrain | last post by:
I am trying to log the Application name, Method name, line number and column number whenever an exception is generated in an C# 2005 application using the following code. Problem is that the...
2
by: =?Utf-8?B?QXJ0aHVyIFBhcmtlcg==?= | last post by:
I've been having a problem occasionally with exceptions having the wrong line number listed. The situation is this: I have a function with a try-catch, and in the catch it logs the message and...
11
by: Horacius ReX | last post by:
Hi, I have to search for a string on a big file. Once this string is found, I would need to get the number of the line in which the string is located on the file. Do you know how if this is...
4
by: mercuryshipzz | last post by:
Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... ..
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.