Home > Archive > Java Help > October 2005 > Converting int to string
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Converting int to string
|
|
|
| Can anyone tell me how to develop this so that it shows the months ie
January, February etc when it runs.
class DayCounter {
public static void main(String[] arguments) {
int yearIn = 2004;
int monthIn = 1;
if (arguments.length > 0)
monthIn = Integer.parseInt(arguments[0]);
if (arguments.length > 1)
yearIn = Integer.parseInt(arguments[1]);
System.out.println(monthIn + "/" + yearIn + "has " + countDays(monthIn,
yearIn) + " days.");
}
static int countDays(int month, int year) {
int count = -1;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if (year % 4 == 0)
count = 29;
else
count = 28;
if ((year % 100 == 0) & (year % 400 != 0))
count = 28;
}
return count;
}
}
| |
| billytcj@yahoo.com 2005-10-29, 7:02 pm |
| To implement the mentioned matter, it is easy to utilize the
java.util.Date & java.util.Calendar package offered in J2SE.
| |
|
|
"Nick" <nickmacdonaldw@hotmail.com> wrote in message
news:1130603754.443476.147260@g44g2000cwa.googlegroups.com...
> Can anyone tell me how to develop this so that it shows the months ie
> January, February etc when it runs.
>
> class DayCounter {
> public static void main(String[] arguments) {
> int yearIn = 2004;
> int monthIn = 1;
> if (arguments.length > 0)
> monthIn = Integer.parseInt(arguments[0]);
> if (arguments.length > 1)
> yearIn = Integer.parseInt(arguments[1]);
> System.out.println(monthIn + "/" + yearIn + "has " + countDays(monthIn,
> yearIn) + " days.");
> }
>
> static int countDays(int month, int year) {
> int count = -1;
> switch (month) {
> case 1:
> case 3:
> case 5:
> case 7:
> case 8:
> case 10:
> case 12:
> count = 31;
> break;
> case 4:
> case 6:
> case 9:
> case 11:
> count = 30;
> break;
> case 2:
> if (year % 4 == 0)
> count = 29;
> else
> count = 28;
> if ((year % 100 == 0) & (year % 400 != 0))
> count = 28;
> }
> return count;
> }
> }
>
As it happens, I developed a simple static method to do exactly what you
want a few years back. Here's the code:
----------------------------------------------------------------------------
-----------------
/**
* Method getMonthEnglish() returns the full English month name
corresponding to
* a 0-based month number.
*
* @since 2003
*
* @return the English month name for a given 0-based month number
*/
static public String getMonthEnglish(int monthNumber) {
String[] MONTH_NAMES_ENGLISH = { "January", "February",
"March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
return MONTH_NAMES_ENGLISH[monthNumber];
}
----------------------------------------------------------------------------
-----------------
To invoke this method, just imitate this example:
int monthNumber = 0;
String monthName = getMonthEnglish(monthNumber);
System.out.println("Month number = " + monthNumber + "; Month name = " +
monthName);
All of this assumes that you are only concerned with English month names;
better code would look up the month name based on Locale so that month 0
would be 'janvier' in French, 'januar' in German, etc.
Rhino
| |
| Roedy Green 2005-10-30, 3:57 am |
| On 29 Oct 2005 09:35:54 -0700, "Nick" <nickmacdonaldw@hotmail.com>
wrote, quoted or indirectly quoted someone who said :
>Can anyone tell me how to develop this so that it shows the months ie
>January, February etc when it runs.
the basic idea is to create a 12-long array constant containing the
months of the year. You can then index it by 0-based month number to
get a string.
See learn how to create array constants see
http://mindprod.com/jgloss/gotchas.html#ARRAY
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
| |
|
| Rhino,
I'm only a short way into learning Java with no previous programming
experience so while at first sight my questions may seems somewhat
simple your response is very helpful as it allows me to develop an
understanding of the wider concepts so thank you very much.
Nick
|
|
|
|
|