• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ExamLab question about String formatting

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the questions I had in the practice exams is the output of this:



The answer is: 1213%1$s

Can anyone explain what this code is doing?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this the whole statement? I mean it is not closing so is there something else that goes with it?
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the second and third arguments to printf are considered as plain strings and the first argument (12%1$s) contains the formatting code in which %1$s -----> is replaced by first argument which is of type String "13%1$s" and the output is:
1213%1$s

IF you had this (12%2$s) in first argument , the output is :1214%1$s

hope you got it
 
Carrie Hall
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansi Agarwal wrote:Is this the whole statement? I mean it is not closing so is there something else that goes with it?



Oops sorry! That was my copy and paste mishap.. I've updated it now.

Don Redd wrote:the second and third arguments to printf are considered as plain strings and the first argument (12%1$s) contains the formatting code in which %1$s -----> is replaced by first argument which is of type String "13%1$s" and the output is:
1213%1$s

IF you had this (12%2$s) in first argument , the output is :1214%1$s

hope you got it



Ah! I get it now! Thanks so much
 
reply
    Bookmark Topic Watch Topic
  • New Topic