java printf

Discussion in 'Java' started by Alex Polite, Jun 5, 2004.

  1. Alex Polite

    Alex Polite Guest

    I'm just starting out with Java.

    Google tells me there's no function/method in the java standard
    library the works Cs printf.

    I find this hard to believe.

    Do I really have to do stuff like:
    String a = b + " c " + d + " e";

    instead of

    String a = "b %s d %s" % (c,e);

    alex

    --
    Alex Polite
    http://polite.se
    Alex Polite, Jun 5, 2004
    #1
    1. Advertising

  2. Alex Polite schrieb:

    > I'm just starting out with Java.
    >
    > Google tells me there's no function/method in the java standard
    > library the works Cs printf.
    >
    > I find this hard to believe.


    Belive it. E.g. Cs printf uses a variable argument list (ellipsis).
    There's nothing like this is Java. Of course, you're free to implement
    an equivalent to printf.

    >
    > Do I really have to do stuff like:
    > String a = b + " c " + d + " e";
    >
    > instead of
    >
    > String a = "b %s d %s" % (c,e);


    Apart from that I don't see printf here:

    String a = "b " + c + " d " + e;

    Bye
    Michael
    Michael Rauscher, Jun 5, 2004
    #2
    1. Advertising

  3. Alex Polite wrote:
    > I'm just starting out with Java.
    >
    > Google tells me there's no function/method in the java standard
    > library the works Cs printf.
    >
    > I find this hard to believe.
    >
    > Do I really have to do stuff like:
    > String a = b + " c " + d + " e";
    >
    > instead of
    >
    > String a = "b %s d %s" % (c,e);
    >
    > alex
    >

    Yes, you do.

    Steve
    Steve Horsley, Jun 5, 2004
    #3
  4. Alex Polite

    Alex Polite Guest

    On lör, jun 05, 2004 at 09:03:28 +0200, Michael Rauscher wrote:

    > Apart from that I don't see printf here:


    Sorry. I was thinking in python.

    --
    Alex Polite
    http://polite.se
    Alex Polite, Jun 5, 2004
    #4
  5. "Alex Polite" <> wrote in message
    news:...
    > I'm just starting out with Java.
    >
    > Google tells me there's no function/method in the java standard
    > library the works Cs printf.
    >
    > I find this hard to believe.
    >
    > Do I really have to do stuff like:
    > String a = b + " c " + d + " e";
    >
    > instead of
    >
    > String a = "b %s d %s" % (c,e);
    >
    > alex
    >
    > --
    > Alex Polite
    > http://polite.se


    As of 1.5 it is possible in Java.

    Silvio Bierman
    Silvio Bierman, Jun 5, 2004
    #5
  6. Alex Polite

    Roedy Green Guest

    On 5 Jun 2004 18:52:49 GMT, Alex Polite <> wrote or quoted
    :

    >
    >I find this hard to believe.


    see http://mindprod.com/jgloss/printf.html


    There are printf packages but mostly you use the + concatenation
    operator instead which C did not have.

    So you say something like this:


    System.out.println( "x:" + x + " y:" + y );

    instead of:

    System.out.printf( "x: %d y: %d", x, y );
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 5, 2004
    #6
  7. Alex Polite

    Roedy Green Guest

    On 5 Jun 2004 18:52:49 GMT, Alex Polite <> wrote or quoted
    :

    >Do I really have to do stuff like:
    >String a = b + " c " + d + " e";
    >
    >instead of
    >
    >String a = "b %s d %s" % (c,e);


    What I find peculiar is that you find the second preferable. You must
    remember the magic %x codes, and you must visually match them up. You
    don't find out till run time if you used the wrong code or left one
    out.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 5, 2004
    #7
  8. Alex Polite

    Neal Gafter Guest

    Look in

    http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html

    and you'll see printf and related methods.

    Alex Polite wrote:
    > I'm just starting out with Java.
    >
    > Google tells me there's no function/method in the java standard
    > library the works Cs printf.
    >
    > I find this hard to believe.
    >
    > Do I really have to do stuff like:
    > String a = b + " c " + d + " e";
    >
    > instead of
    >
    > String a = "b %s d %s" % (c,e);
    >
    > alex
    >
    Neal Gafter, Jun 5, 2004
    #8
  9. Ronny Schuetz, Jun 5, 2004
    #9
  10. Silvio Bierman schrieb:

    > "Alex Polite" <> wrote in message
    > news:...
    >
    >>I'm just starting out with Java.
    >>
    >>Google tells me there's no function/method in the java standard
    >>library the works Cs printf.

    [...]
    >
    >
    > As of 1.5 it is possible in Java.


    One never stops to learn :)

    Thank you.

    Bye
    Michael
    Michael Rauscher, Jun 5, 2004
    #10
  11. Alex Polite

    Jezuch Guest

    U¿ytkownik Alex Polite napisa³:
    > I'm just starting out with Java.
    >
    > Google tells me there's no function/method in the java standard
    > library the works Cs printf.
    >
    > I find this hard to believe.


    I find it hard to believe that somebody *really* wants printf :>

    > Do I really have to do stuff like:
    > String a = b + " c " + d + " e";
    >
    > instead of
    >
    > String a = "b %s d %s" % (c,e);


    Java's "style" is more similar to C++'s ostream::eek:perator<<()
    printf is not typesafe (although gcc can warn you about mismatches between
    format string and argument list) and Java tries (tried?) to avoid this kind
    of error-prone stuff.
    --
    Ecce Jezuch
    "But it's not real and that's why its how I always want to feel
    so let's die, before the secret gets revealed
    I've tried but nothing ever can appeal
    and if you don't mind I'd like to throw it all away" - J. Stem
    Jezuch, Jun 7, 2004
    #11
  12. Roedy Green sez:
    > On 5 Jun 2004 18:52:49 GMT, Alex Polite <> wrote or quoted
    >:
    >
    >>Do I really have to do stuff like:
    >>String a = b + " c " + d + " e";
    >>
    >>instead of
    >>
    >>String a = "b %s d %s" % (c,e);

    >
    > What I find peculiar is that you find the second preferable. You must
    > remember the magic %x codes, and you must visually match them up. You
    > don't find out till run time if you used the wrong code or left one
    > out.


    You know perfectly well that string concatenation, unlike printf()
    or C++ ostream << operator works only on strings. Plus, it pretends
    to mutate an immutable object, creating a whole bunch of temporaries
    in the process. And that sucks because Java String is not exactly
    a light-weight type, and because temporaries don't ge destroyed
    until gc gets a round tuit.

    So I find it peculiar that you find concatenation preferrable.

    Not to mention the fact that
    String foo = "green", bar = "apples";
    printf( "%d %s %s", 7, foo, bar )
    is more compact than either
    StringBuffer buf = new StringBuffer();
    buf.append( 7 );
    buf.append( " " );
    buf.append( foo );
    buf.append( " " );
    buf.append( bar );
    or
    String baz = new Integer( 7 ).toString() + " " + foo + " " + bar;

    But you knew that already.

    Dima
    --
    Q276304 - Error Message: Your Password Must Be at Least 18770 Characters
    and Cannot Repeat Any of Your Previous 30689 Passwords -- RISKS 21.37
    Dimitri Maziuk, Jun 7, 2004
    #12
  13. Alex Polite

    Roedy Green Guest

    On Mon, 7 Jun 2004 16:19:40 +0000 (UTC), Dimitri Maziuk
    <dima@127.0.0.1> wrote or quoted :

    >You know perfectly well that string concatenation, unlike printf()
    >or C++ ostream << operator works only on strings. Plus, it pretends
    >to mutate an immutable object, creating a whole bunch of temporaries
    >in the process. And that sucks because Java String is not exactly
    >a light-weight type, and because temporaries don't ge destroyed
    >until gc gets a round tuit.


    Concatenation automatically invokes toString, which is exactly what
    happens inside printf.

    As for littering the world with String object temporaries, that
    depends on your compiler. A smart compiler like Jet will allocate
    those on the stack, just the way printf would.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 7, 2004
    #13
  14. Alex Polite

    Sergio Guest

    Dimitri Maziuk wrote:

    > Not to mention the fact that
    > String foo = "green", bar = "apples";
    > printf( "%d %s %s", 7, foo, bar )
    > is more compact than either
    > StringBuffer buf = new StringBuffer();
    > buf.append( 7 );
    > buf.append( " " );
    > buf.append( foo );
    > buf.append( " " );
    > buf.append( bar );
    > or
    > String baz = new Integer( 7 ).toString() + " " + foo + " " + bar;
    >




    System.out.println(7+" "+foo+" "+bar);

    kind of compact, right?
    Sergio, Jun 7, 2004
    #14
  15. Alex Polite

    Andrew Guest

    >Alex Polite wrote:

    > Google tells me there's no function/method in the java standard
    > library the works Cs printf.
    >
    > I find this hard to believe.


    Of course, Java does have a C-like printf now! J2SE 1.5 is introducing
    System.out.printf().

    Cheers,
    Andrew
    Andrew, Jun 8, 2004
    #15
  16. Alex Polite

    Liz Guest

    "Alex Polite" <> wrote in message
    news:...
    > I'm just starting out with Java.
    >
    > Google tells me there's no function/method in the java standard
    > library the works Cs printf.
    >
    > I find this hard to believe.
    >

    Ya well google is the GOD of programming. If she can't do it, nobody can.
    Liz, Jun 8, 2004
    #16
  17. Alex Polite () wrote:
    : I'm just starting out with Java.

    : Google tells me there's no function/method in the java standard
    : library the works Cs printf.

    try MessageFormat()

    not a printf clone, but similar idea.

    --

    (Paying) telecommute programming projects wanted. Simply reply to this.
    Malcolm Dew-Jones, Jun 9, 2004
    #17
  18. Alex Polite

    Gerry Murphy Guest

    That's useful to know. I just inherited a large Java
    codebase that's using a third-party library to
    use printf, sprintf and fprintf, etc. and I'd like to remove them
    someday.

    Thanks.

    "Dale King" <> wrote in message
    news:...

    > As others have already mentioned, there is going to be a printf
    > style formatting API in 1.5 (along with something equivalent to
    > scanf, but based on regex). But to your point here there is also
    > going to be support for variable length argument lists in 1.5.
    >
    > And to the OP there is also java.text.MessageFormat that also
    > does some of what you would do with printf. It allows you to
    > specify a format string with markers specifying where to insert
    > the arguments. It however does not give you fine control over how
    > that argument is formatted.
    > --
    > Dale King
    > My Blog: http://daleking.homedns.org/Blog
    Gerry Murphy, Jun 18, 2004
    #18
  19. Alex Polite

    Dale King Guest

    Hello, Michael Rauscher !
    You wrote:

    > Alex Polite schrieb:
    >
    > > I'm just starting out with Java.
    > >
    > > Google tells me there's no function/method in the java

    standard
    > > library the works Cs printf.
    > >
    > > I find this hard to believe.

    >
    > Belive it. E.g. Cs printf uses a variable argument list

    (ellipsis).
    > There's nothing like this is Java. Of course, you're free to

    implement
    > an equivalent to printf.


    As others have already mentioned, there is going to be a printf
    style formatting API in 1.5 (along with something equivalent to
    scanf, but based on regex). But to your point here there is also
    going to be support for variable length argument lists in 1.5.

    And to the OP there is also java.text.MessageFormat that also
    does some of what you would do with printf. It allows you to
    specify a format string with markers specifying where to insert
    the arguments. It however does not give you fine control over how
    that argument is formatted.
    --
    Dale King
    My Blog: http://daleking.homedns.org/Blog
    Dale King, Apr 15, 2006
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn
    Replies:
    1
    Views:
    706
    Christopher Benson-Manica
    Dec 14, 2006
  2. ben
    Replies:
    4
    Views:
    614
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,240
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    431
  5. guru
    Replies:
    8
    Views:
    278
Loading...

Share This Page