Textual Equivalent

J

John E

What is the best way to store a string equivalent of an integer in Java?

"January" = 1
"February" = 2

etc...

TIA
 
J

Jarmo

John E said:
What is the best way to store a string equivalent of an integer in Java?

"January" = 1
"February" = 2

etc...

TIA

Using a Hashtable? For example:

Hashtable hash = new Hashtable();
hash.put("January", new Integer(1));
hash.put("February", new Integer(2));

However if you genuinely are just working with months then you might be
coming at this from the wrong angle. Typically you'd use integers to
represent the months and then have an array of month-names.
 
J

John E

Jarmo said:
Using a Hashtable? For example:

Hashtable hash = new Hashtable();
hash.put("January", new Integer(1));
hash.put("February", new Integer(2));

However if you genuinely are just working with months then you might be
coming at this from the wrong angle. Typically you'd use integers to
represent the months and then have an array of month-names.

What if I had to randomly generate two possible textual values? e.g. "Heads"
and "Tails"?
 
F

Flip

"January" = 1
Can't you do something like

// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar = new GregorianCalendar(pdt);
Date trialTime = new Date();
calendar.setTime(trialTime);

// print out a bunch of interesting things
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));

This was taken from
http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top