Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using printf within println?

 
Josh Priest
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.prinln("I want this one to be long " + (3/10) + " and this one to be shorter " + printf(3/10, .2f))

Is there a way to do that midsteam, or should I just use printf instead of println and end it with a \n?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Josh,

your example is syntactically incorrect. Please have a look at the API doc for the printf() method

But for your real question: You can nest the printf() (it's the one from Sytem.out, right?) method into the println() method but it won't do what you expect. A String is needed here so the toString() method of System.out (=PrintStream) is called which doesn't produce the output you're looking for.

Personally I don't see why you would want to nest the print methods anyway?!? You can use a single printf() with the placeholders and formattings you need. Alternatively you could use the Formatter class to produce the formatted output you want which can then be stored as a String in reused in output statements.

Marco
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use printf and end with %n. That will use the system line terminator, so possibly \r or \r\n.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Sorry, I have missed the point with the line terminator...

Maybe you would even want to use the system property available on all JVMs which gives you the OS specific line terminator.

Marco
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Ehrentreich wrote:Oops. Sorry, I have missed the point with the line terminator...
Maybe you would even want to use the system property available on all JVMs which gives you the OS specific line terminator.


Is this necessary/appropriate when all you're doing is printing to the console? Please correct me if I'm wrong, but I thought that that OS specific line terminators are needed when writing out a text file. Thanks.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general every text based tool or data format has to deal with such issues. Caring about such portability issues is some additional work which is probably not really necessary for simple console output for debugging purposes. Moreover most editors and other tools are capable enough to deal with different line endings.

On the other hand I think you should keep it in mind so you can make your application more portable where it is really necessary. Often developers tend to ignore such small issues (not only regarding portability) completely and wonder why an application sometimes work and sometimes it doesn't. But of course this is only my opinion.

Marco
 
Josh Priest
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I am not as concerned about the new line char as much as figuring out the printf format really.


How do I handle multiple numbers of different percision within the same printf statement?

I think I found the docs here http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf%28java.util.Locale,%20java.lang.String,%20java.lang.Object...%29, and I guess the args are passed after , at the end?

Something like this?

Sustem.out.printf("Number 1 is: " + %.2f + "Number 2 is: " + %.1f + "Number 3 is: " + %.5f + "\n", num1 num2 num3)
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I am not as concerned about the new line char as much as figuring out the printf format really.

Of course there's no use in debating about the new line character It was just an advice for other situations...

Your example for the printf() method is no exactly correct. You write the placeholders for the numbers you want to print directly inside the String text, i.e. not like "bla bla " + %.2f + " foo bar" but instead one single String "bla bla %.2f foo bar".

The values for the placeholders are given as comma separated parameters after this String. This is a var-args parameter which means you can give an arbitrary number of parameters to this method.

Additionally I should perhaps note that you can mix any type of placeholder values and not only values of the same type.

Marco
 
Josh Priest
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I wrote it as:

Sustem.out.printf("Number 1 is: %.2f Number 2 is: %.1f Number 3 is: %.5f \n", num1, num2, num3)

I think that did the trick.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad that it works for you now!

As I mentioned above you can easily use the Formatter class which uses the same placeholder syntax. The advantage would be that you can save the formatted String in variable if needed.

Marco
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh,

Now that you have got the hang of printf, start using %n for newline rather than \n. printf I believe uses Formatter as they are identical in syntax.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic