• 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

format

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Ques02 {
public static void main(String[] args) {
int anInt = 100;
double aDouble = 100.00;
System.console().format("%2d - %1f", anInt, aDouble);
}
}


A. The program will output 100.000000 - 100.
B. The program will output 100 - 100.000000.
C. The program will throw a IllegalFormatConversionException at run-time.
D. The program will output 100 - 100.

Correct answer: B

Here the ordering of the arguments (anInt and aDouble) matters, not the formatting strings %2 - %1d. So the first output will be the value of anInt variable followed by a hyphen (-) followed by the value of aDouble variable.

Why?

Well, I know that "%2d - %1f" != "%2$d - %1$f" (in this case the output would be "100.000000 - 100"), but what does "%2d - %1f" mean?..


While writing this post I got an idea that "%2d - %1f" could mean minimum length.. Is it so?.. Or I'm on the wrong way?..

Thanks a lot!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andrew Fedotov:
public class Ques02 {
public static void main(String[] args) {
int anInt = 100;
double aDouble = 100.00;
System.console().format("%2d - %1f", anInt, aDouble);
}
}


A. The program will output 100.000000 - 100.
B. The program will output 100 - 100.000000.
C. The program will throw a IllegalFormatConversionException at run-time.
D. The program will output 100 - 100.

Correct answer: B

Here the ordering of the arguments (anInt and aDouble) matters, not the formatting strings %2 - %1d. So the first output will be the value of anInt variable followed by a hyphen (-) followed by the value of aDouble variable.

Why?

Well, I know that "%2d - %1f" != "%2$d - %1$f" (in this case the output would be "100.000000 - 100"), but what does "%2d - %1f" mean?..


While writing this post I got an idea that "%2d - %1f" could mean minimum length.. Is it so?.. Or I'm on the wrong way?..

Thanks a lot!



Yes, the integer is the minimum number of characters to be written to the output.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting a null pointer exception,in the following line
System.console().format("%2d - %1f", anInt, aDouble);
Can anyone explain.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic