wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Printf (not enough resources) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Printf (not enough resources)" Watch "Printf (not enough resources)" New topic
Author

Printf (not enough resources)

peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
hello there to everybody ! ...
i am giving the exam on Saturday but i still have some problems understanding easy stuff !
like printf()
whatever i have read in Books the resources they offer are not enough and i can't understand what the @D#@D#@ it does
like this
//Devaka Question
System.out.printf("%f is the best value %b",Math.PI,Math.E);
System.out.printf("%+(04.2f",12.6542);

what is %d ? and what is %f ?
//Whizlabs Question
double val1=3.91;
double val2=3.97;
System.out.printf("%.1f%.1f",val1,val2);


what do i accomplish by using printf ? please someone help me on this ! it might look simple but this thing is bugging me off all the time and i am
missing questions when i try the devaka and whizlabs Exams


Time is relative so there is no way i can be late.
~Albert Einstein~
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
The printf function is as old as dirt (or C.) Basically, it uses a string format (which is a string which may contain both literal characters and format specifications, like %f and %b) along with additional arguments. The idea is that each argument after the format string is matched to a format specification, and converted to a string according to the format specification. For example, %f will convert the corresponding argument to a floating point string representation, and %b will convert the argunent to either "true" or "false" depending on some conditions. There are a lot of rules (you need to look at the java.util.Formatter Sun API docs, where all of this is specified.) But since you are taking the exam this Saturday, you probably don't have time to that. I recommend in that case that you review what you read in your books and ask if you have any questions. I think K&B covers this sufficiently well for the exam (but not exhaustively.) For exhaustive coverage, refer to the java.util.Formatter documentation. But I don't recommend it with your exam being so close.


All code in my posts, unless a source is explicitly mentioned, is my own.
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
Do you know what %b this stands for ?
it always prints true ?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
No, it's not always "true". Otherwise, it would be a completely useless format. From java.util.Formatter API docs:


If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
Can you give me an Example ?
it would be very Helpful
Best Regards....
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
There you have it:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printf (not enough resources)