aspose file tools*
The moose likes Beginning Java and the fly likes using printf within println? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using printf within println?" Watch "using printf within println?" New topic
Author

using printf within println?

Josh Priest
Greenhorn

Joined: Jun 06, 2007
Posts: 10
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

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Use printf and end with %n. That will use the system line terminator, so possibly \r or \r\n.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Jun 06, 2007
Posts: 10
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

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Jun 06, 2007
Posts: 10
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

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Jan 29, 2010
Posts: 85
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.


Rajeev Trikha (SCJP 6)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using printf within println?