This question already has answers here:
Is there a good reason to use "printf" instead of "print" in java?
(4 answers)
Closed 8 years ago.
Just came to know that java does has a method named printf, then what is the difference between printf & println?
System.out.println(); is efficient for simply printing a line of text. If the line of text needs to be formatted (ex: alignment (left-justified, etc.), etc.), then System.out.printf(); would be used.
Check out this link for more information.
Related
This question already has answers here:
Is there a good reason to use "printf" instead of "print" in java?
(4 answers)
Closed 3 years ago.
Basically i am a noob at programming but one of the first exercises we had was we had to use printf() which i found really difficult so i was wondering is there any real advantage to using printf() over print() with concatenation signs.
With printf() you can do a lot of advanced formatting which print() can't do
Check this out
This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 7 years ago.
The title says it all. Right now if I input a number like 100.50, in my program it prints as 100.5. Is there an easy way to make the program recognize the zero?
You can do this trick.
String s = String.format("%.2f", 100.50);
This question already has answers here:
Convert String to KeyEvents
(16 answers)
Closed 7 years ago.
I need a way to do what is described in the first answer to this question: Type a String using java.awt.Robot
Only I would like to avoid using the clipboard. Is there a generic way to do it without?
(Other answers to the question address printing some hard-coded keys, but they don't help me print "Hello, world!")
You can use javax.swing.KeyStroke to transform the characters in your string into keycodes. For each key code, call Robot.keyPress(...), Robot.keyRelease(...) as you are doing in your previous question
This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
Closed 9 years ago.
I have a string like :
"\"[\"a\",\"b\", \"c\"]\""
How to convert this to a list of strings
Could you suggest me a nice way of doing it in Java?
str.contains("c") does this job. However did not you think to consult String class documentation first?
Use contains():
if (str.contains("\"c\""))
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)