Print current line number in intellij [duplicate] - java

This question already has answers here:
How can we print line numbers to the log in java
(22 answers)
Closed 3 years ago.
I am trying to print the current line number(line number of ide) of a certain code from intellij. How do I do that?
1 System.out.print(getCurrentLineNo());//sample code
2 System.out.print(getCurrentLineNo());//sample code
3 System.out.print(getCurrentLineNo());//sample code
4 System.out.print(getCurrentLineNo());//sample code

One way to do this is to get the stack trace element and call getLineNumber.
public static int getCurrentLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
Note that I used an index of 2 because that refers to the frame of the method that calls getCurrentLineNumber. If you are just doing this inline, i.e. like this:
System.out.println(Thread.currentThread().getStackTrace()[1].getLineNumber());
You should use an index of 1 instead.
Note that an index of 0 refers to the frame of the getStackTrace method.

I suppose it too expensive to get StatckTrace for that, but it can help you for some debug.
System.out.println(new Exception().getStackTrace()[0].getLineNumber());
Also, you can look here

You can get line number like this...
public static int getCurrentLineNo() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}

Related

Can someone explain this line [duplicate]

This question already has answers here:
Difference between moving an Iterator forward with a for statement and a while statement
(5 answers)
Closed 5 years ago.
hello i'm trying ton understand a programm and a line giving me some trouble. Can you explain me the Construction of this line because i have never seen a "For" like that
for (Iterator<IBaseVO> itMachine = machinesSelected.iterator(); itMachine.hasNext();) {
MachineVO mach = (MachineVO) itMachine.next();
idsMachines.add(mach.getMchId());
}
thanks you
A for loop can have 3 arguments, but it's not required.
Usually goes like this
instruction
boolean
instruction
the first and the third one can be omitted safely.
So here is the first instruction slot used to initialize the iterator object.
The second instruction slot is used to see if there are new items in the iterator.
the third slot is not needed and thusly omitted.
This does the same as
Iterator<IBaseVO> itMachine = machinesSelected.iterator();
while (itMachine.hasNext()) {
MachineVO mach = (MachineVO) itMachine.next();
idsMachines.add(mach.getMchId());
}

Java Array returning weird output [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
I'm creating a method for a homework problem that returns the highest value in an array. I'm using a for
loop to input the numbers into the array. Here is what the input code looks like.
int[] array = new int [n];
for(i=0;i<array.length;i++){
array[i]=sc.nextInt();
System.out.println(array);}
When i run the program to try and see if there's anything wrong as a I go along I notice the array not returning the value I input into it. For example
putting the number 12 it returns [I#6c80d028. I'm pretty new to java so I don't really know what's going on with the input. Is there any thing that can be done?
Please use:
System.out.println(Arrays.toString(array));
Please do this
System.out.println(array[i]);
or
System.out.println(Arrays.toString(array));
when you
System.out.println(array);
It prints the address and not the value hence you see a junk characters / not expected value.

How to write a counter for a .txt file in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a program that I am writing and I have it completed but I keep receiving an error in a counter that I am using. The purpose of the program is to build a plate stacker and I have finished that but need to write a counter based on the particular colors used (how many green plates...etc.). As I know several classmates use this site to cheat, I will not post the code in whole but only the fragments I feel are necessary. Below is an example of a statement that I am using to attempt to get a count of the beige plates in the text file. The statement is in a while loop with the other colors having the same coding. I also have initialized the counter (int beige = 0;) prior to this and outside of the while loop:
if(line == "beige")
{
beige++;
}
I have a System.out following these statements to display the count but it always comes up as 0 so I believe the problem is in this particular fragment. If I need to send additional coding I will but I believe it is something to do with this or the placement of it in the while loop.
First, you are comparing the string in a wrong way. Use .equals method.
if (line.equals("beige"))
beige++;
Second, you can use HashMap for this kind of problem where you keep updating the value(count) for key(color) and it will save you a lot of if conditions if say there are a hundred different colors!

How to get the name of the containing function [duplicate]

This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 8 years ago.
In C++, you can use __FUNCTION_NAME__ to get the name of the function that contains __FUNCTION_NAME__.
Is there an equivalent in Java? It could, in Java, be possible to do something with this and reflection. Is there something simpler though?
Thread.currentThread().getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html
Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();
depth = 0 (zero) will give current method
also
System.out.println((new Throwable()).getStackTrace()[0].toString());
Sample output:
com.junk.Junk3.main(Junk3.java:12)

What is the meaning of the three dots in a method declaration? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java, 3 dots in parameters
public static void getImages(String... folders) throws IOException{
}
In the above getImages() method, why there is three dots. What is it mean? I searched google but couldn't find anything.
Yeah, punctuation is hard to search for if you don't know the technical term. In this case, it is varargs. Here is a nice link to explain it. Basically, the caller can add as many arguments as desired, and the method sees them arriving as an array of that length.

Categories

Resources