• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printf (not enough resources)

 
peter kosmas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
peter kosmas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what %b this stands for ?
it always prints true ?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me an Example ?
it would be very Helpful
Best Regards....
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you have it:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic