Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

System.out.println()

Thread view: 
Roy Gourgi - 24 Oct 2006 10:49 GMT
Hi,

I am trying to format my output with System.out.println.

I have declared:

public double gnShortLosses = 1330.03;
public double gnLongLosses = 17770.330;

and I would like to print them out with System.out.println() so that each
one of the 2 double variables takes a width of 15 characters. Furthermore,
how can I print them out with the appropriate commas if possible. For
example I would like gnShortLosses to print out like this "       1,330.03".
Because 1,330.03 takes 8 spaces, I would like to have 7 blanks in front of
it (i.e. 15 characters) and the comma in the right place.

TIA
Roy
Hendrik Maryns - 24 Oct 2006 11:04 GMT
Roy Gourgi schreef:
> Hi,
>
[quoted text clipped - 11 lines]
> Because 1,330.03 takes 8 spaces, I would like to have 7 blanks in front of
> it (i.e. 15 characters) and the comma in the right place.

Have a look at Formatter and String.format

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roy Gourgi - 24 Oct 2006 11:40 GMT
Hi Hendrik,

I tried Formatter and String.format but I am a bit confused??

Can you just show me how the statement with System.out.println();

Roy

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 36 lines]
> =jhaM
> -----END PGP SIGNATURE-----
Hendrik Maryns - 24 Oct 2006 11:48 GMT
Roy Gourgi schreef:
> Hi Hendrik,
>
> I tried Formatter and String.format but I am a bit confused??

If you read the javadoc for Formatter (it is here:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html), you
would have seen that there is
System.out.format(String, Object...)

The syntax is described in Formatter, it would probably be something like

System.out.format("%15f", gnShortLosses);

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roy Gourgi - 24 Oct 2006 12:45 GMT
Hi,

I tried that but I get the error message:

 a.. "Strategy.java": cannot find symbol; symbol : method
format(java.lang.String,int), location: class java.io.PrintStream at line
367, column 22
I have imported the following:

import java.util.*;
import java.io.*;
import java.lang.*;

Why does it not work?

Roy

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 28 lines]
> =6yMk
> -----END PGP SIGNATURE-----
Hendrik Maryns - 24 Oct 2006 14:32 GMT
Please do not top-post.

Roy Gourgi schreef:
> Hi,
>
[quoted text clipped - 10 lines]
>
> Why does it not work?

You don’t show enough code, but here’s what my cristall ball says:

You still have to say which object you are invoking format on.  You’ll
have to sype the System.out.

H.

> Roy Gourgi schreef:
>>>> Hi Hendrik,
[quoted text clipped - 10 lines]
>
> H.

- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roy Gourgi - 24 Oct 2006 14:54 GMT
This in essence is my code, as I declare a gnShortLosses as double and then
I try to print it out but it does not work?
Sorry but I am new to Java and when you say sype the System.out I do not
know what you mean!

import java.util.*;
import java.io.*;
import java.lang.*;

 public static void main(String[] args)
   {

       public double gnShortLosses = 0.0;

       System.out.format("%15f", gnShortLosses);

  }

Thanks
Roy

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 53 lines]
> =/Kki
> -----END PGP SIGNATURE-----
Hendrik Maryns - 24 Oct 2006 15:26 GMT
Once again, please do not top-post!

Roy Gourgi schreef:
> This in essence is my code, as I declare a gnShortLosses as double and then
> I try to print it out but it does not work?
> Sorry but I am new to Java and when you say sype the System.out I do not
> know what you mean!

That of course was a typo, I meant ‘type’.

> import java.util.*;
> import java.io.*;
[quoted text clipped - 8 lines]
>
>    }

This cannot be your code, because the class declaration fails.  This
means I cannot paste this into my editor and compile it.  So I won’t.
Seems like it should work to me, but once again, *read the docs* of
Formatter, and you should be able to find out.

Btw, you don’t need any of the imports above.

H.

> Please do not top-post.
>
[quoted text clipped - 34 lines]
>>>>
>>>> H.

- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Tor Iver Wilhelmsen - 24 Oct 2006 21:12 GMT
> Can you just show me how the statement with System.out.println();

1) Make sure you use JSE 5.0 aka. J2SE 1.5 which has the new formatting stuff

2) use System.out.printf("%15f", gnShortLosses);

If you use older runtimes look into NumberFormat and manual padding.
Roy Gourgi - 24 Oct 2006 22:11 GMT
Thanks Tor that did the trick.

Roy

> Hi,
>
[quoted text clipped - 14 lines]
> TIA
> Roy
Kevin Mess - 25 Oct 2006 13:00 GMT
> Hi,
>
[quoted text clipped - 15 lines]
> TIA
> Roy

Hi Roy.  You didn't say what version of Java you're using, so I have
two solutions.  Hopefully, you're using 1.5, because it's quite simple:

import java.text.DecimalFormat;

class test {

   public static void main(String[] args) {

       double gnShortLosses = 1330.03;
       double gnLongLosses = 17770.330;

       // 1.4 - Left Justified
       DecimalFormat f = new DecimalFormat("#,###,###,###,##0.00");
       System.out.println(f.format(gnShortLosses));
       System.out.println(f.format(gnLongLosses));

       // 1.5 - Optionally right-justified
       System.out.printf("%,15.2f", gnShortLosses);
       System.out.println();
       System.out.printf("%,15.2f", gnLongLosses);
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.