• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Format problem

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80288
433
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out
 
machines help you to do more, but experience less. Experience this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic