wood burning stoves*
The moose likes Beginning Java and the fly likes please explain every line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please explain every line " Watch "please explain every line " New topic
Author

please explain every line

suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17


output

/*
*
The Date for United States:
In FULL is Tuesday, May 9, 2006
In LONG is May 9, 2006
In MEDIUM is May 9, 2006
In SHORT is 5/9/06

The Date for United Kingdom:
In FULL is 09 May 2006
In LONG is 09 May 2006
In MEDIUM is 09-May-2006
In SHORT is 09/05/06

The Date for Germany:
In FULL is Dienstag, 9. Mai 2006
In LONG is 9. Mai 2006
In MEDIUM is 09.05.2006
In SHORT is 09.05.06

The Date for France:
In FULL is mardi 9 mai 2006
In LONG is 9 mai 2006
In MEDIUM is 9 mai 2006
In SHORT is 09/05/06

* */
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Welcome to the Ranch
What you have is an array of locales (or locations) and an array of date formats. You go through the different countries, then for each country you print the date in each of the formats, with the spelling used in that country. Note that in North America the month precedes the day, and in Europe the day precedes the month.

It is unfortunate that one has to use an array of names {"FULL", "SHORT" etc }, rather than having an enum. If the constants were enum members, one could use their toString method to print FULL, SHORT, etc.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
By the way, did you copy that code from anywhere? It looks like code from a book printed in 2006, unless it has taken over 5 years from your running it to asking for the explanation
suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17
actually im just started java, im confused why array is used why dont use method and consructor... im reading from cbt nuggets tutorial, in that always he used method and constructors.. but suddenly how array in coming im confused... please help friends i want to become a good application developer..
suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17
and please explain why particularly int and locale array is used.. how do i find that array in api doc
suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17
is there anybody to help me out
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4372
    
    8

I'd guess that arrays are being used simply because it's an example demonstrating how to use arrays. So it's creating an array of locales an an array of styles (as used by DateFormat.getDateInstance()), and printing out the date in each combination of style and locale.

The Java tutorials have got more about arrays here: http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

suresh krishan wrote:and please explain why particularly int and locale array is used.. how do i find that array in api doc

It's 'Locale', not 'locale' (a good first lesson; compilers are NOT forgiving about that sort of mistake). And if you click on that word in this sentence, you'll see its API.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

suresh krishan wrote:is there anybody to help me out
You realize this is a forum - not a chat, right? waiting 12 whole minutes before begging for someone to answer comes across as a little rude. Everyone on this site is a volunteer, and gives what time they can.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
I don't think the example is demonstrating the use of arrays per se; I think it's demonstrating how you use Locale and DateFormat to produce region-appropriate date strings in a variety of formats. I think arrays are used because they can be instantiated very easily, enabling the author to generate a number of examples in a very short space.

This looks very familiar. I'd bet a doughnut it's from the K&B discussion of DateFormat.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
That shows the hazard of rushing ahead of yourself. You ought to have sought a tutorial about arrays, and read that. Then you could have asked us questions about what you don’t understand, and we would have been keen to help.
suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17
thanks friends... but i dont know how this Locale api is related to this program... dont know what are the things taken this program from Locale api.. could anybody explain in detail..
suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17
In Locale api there only given Locale constructors but in this program Locale array is used.. how?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

you can have an array of any class or interface type. No class has an explicit 'array' constructor.
suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17
why using array String in this program.. i could not find this array in Locale api...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
The String array is for printing out the names of the formats. If there had been an enum for the formats, you could have called toString() on the format objects, but they are only ints, so you have to copy their names into an array.

We can see an enum would have been more object-oriented, but enums hadn’t been introduced into Java™ when the DateFormat class was written.
 
 
subject: please explain every line