File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes another question on arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another question on arrays" Watch "another question on arrays" New topic

another question on arrays

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134

if this is a given array:
Integer[] ir = {12, 45678, 90123, 3};

how do i rewrite this loop:

to get following output:
12 (...e.g. Euro)

i am trying to get the programme to print out numbers (without comma, or decimal separators : as seen in Array "ir" ) as currency sums...
....however: the sums should be formatted the way some european countries do...(i.e. not with commas as is done in the US for example - but with decimal points)...

what functions/methods etc do i need to append (to replace the " *** " )...

i would also appreciate somene explaining to me where (in the API ? ) i can get more help.....

i've looked through the classes: DecimalFormat and Formatter, as well as Locale..... but it's gotten me even more confused...

i'd appreciate some help.


Ajay A Patil

Joined: Apr 13, 2006
Posts: 22
> what functions/methods etc do i need to append (to replace the " *** " )...

DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN);
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802

You down with OOP? Yeah you know me!
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

"Monk Fox", you have previously been warned on multiple occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional, and this is your final warning. Adjust your display name to comply with the required standards prior to your next post.

Failure to comply will result in the removal of your account.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
oh, thanx a million Ajay / Monk....

looks like a workable solution, can anyone tell me how i would do it using "printf" i was told it would be more efficient...(how true is this?) ....and are there any advantages of using "printf" over the way suggested by Ajay/ Monk ?

thanx in advance.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Here is an exmple usinng printf(), although I have no idea about differences in efficiency
The flags & conversion syntax used in the printf() method are located in the documentation for the Formatter class. They are a little confusing as you undoubtedly have already seen. The most straightforward way IMO is using the DecimalFormat class as has already been suggested. But it can be done either way.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
You use letters preceded by % characters in a String to represent an output. So
%d will print out a decimal integer calue of how mush money you should send me.
You can use different figures after the % to have the number take up a certain number of spaces, with or without leading 000 or +-.
%n is new line, and %6.2f is a fixed-point decimal number occupying six spaces, two after the decimal point (eg 123.45 takes up six spaces).
There are hundreds of different combinations available; look in the API spceification for the java.util.Formatter class.
[ April 17, 2006: Message edited by: Campbell Ritchie ]
Fox Trot

Joined: Apr 17, 2006
Posts: 5
lol printf is goofy, just use println or print...

C and C++ use printf....

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
thanx alot everyone,....

you've been a great help.....

I agree. Here's the link:
subject: another question on arrays
It's not a secret anymore!