• Post Reply Bookmark Topic Watch Topic
  • New Topic

Format problem  RSS feed

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I need to print out the table of conversions from kilogram to pound and from pounds to kilograms. I think I have done a while loop correctly, but it is hard to actually check it since I do not have proper output format.
I have tried also %4.2f format option however could not find the proper position in the print.
Can somebody assist me please.

 
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it needs to be something like: System.out.printf ("%.2f", pounds);
so , its pounds, not "pounds" . you are using string value as the second argument
Edited.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You certainly don't want to be using %s because that is for formatting strings, so why don't you show us the code that is using %f formatting and also tell us exactly what is wrong with the output
i.e. what output were you expecting and what output did you get ?
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When dealing with formatted output, the first place to use as a reference is the Formatter class documentation...
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:You certainly don't want to be using %s because that is for formatting strings, so why don't you show us the code that is using %f formatting and also tell us exactly what is wrong with the output
i.e. what output were you expecting and what output did you get ?


here is the picture how it should look like:



And my current printout looks like this:
 
Ngml Bishop
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lovro Bajc wrote:
here is the picture how it should look like:



And my current printout looks like this:


i think this works. remember , %f stands for double and float, %d for integer type, and %s which is not used here, is for string, you can use a type cast as needed.



what i'v done is counting the white space in the first line and set the numbers in format identifers
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ngml Bishop wrote: . . . %f stands for double and float, %d for integer type, and %s which is not used here, is for string, . . .
No %f stands for fixed‑point, and %d stands for decimal integer.
If you go through the Formatter documentation you find you can only use %f with numbers which might need a decimal point (double float and BigDecimal) and %d only with integers. you should read that documentation (even if you immediately forget 99.9% of it) and the two Java Tutorials sections about formatting: try here: No 1 and No 2.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use %s on any reference type; it usually calls its toString() method.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help. With it I cleared up a bit mess in my head . Thanks a lot
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!