Results 1 to 10 of 10
5Likes Thread: Month class
- 07-13-2012, 05:55 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Month class
Hi All! Here is the exercise which asks to transform numbers from 1 to 12 into the corresponding month names january, feb and etc. Need to implement class whose constructor parameter is the month number and whose getName method returns the month name. It also gives hint: Make a very long string "January February....", in which you add spaces such that each month name has the same length.Then use substring to extract the month you want.
basically, I just have the beginning only:
Month class:
and MonthTester class:Java Code:import java.util.Scanner; public class Month { public Month() { Scanner in = new Scanner(System.in); System.out.println("Please enter month digit"); digit = in.nextInt(); } public String getName() { String monthName = new String("JanFebMarAprMayJunJulAugSepOctNovDec"); String january = monthName.substring(0,2); String february = monthName.substring(3,5); //the same method for other months as above return monthName; } private int digit; }
Ok, I understand what should I do but I cannot digit with the month I need, what should I use here? Integer.parseInt, Integer.toString? How to connect them? Thanks for help!Java Code:public class MonthTester { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Month month = new Month(); month.getName(); } }
- 07-13-2012, 06:01 AM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Month class
Are you required to follow the "hint"?
I ask because this seems like a very complex way of going about a simple task. A better way to solve the problem is by utilizing an array: Arrays (The Java Tutorials > Learning the Java Language > Language Basics)"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-13-2012, 06:36 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Re: Month class
I am following the book, I didn't study arrays yet, so I better to use the hint :)
- 07-13-2012, 06:57 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
Re: Month class
It looks like this is an exercise in loops. The first thing you should do is fix your month string. Since "September" is the largest name, you have to pad your month names to 9 spaces: "January February March April May ...
- 07-13-2012, 07:02 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: Month class
From this: String monthName = new String("JanFebMarAprMayJunJulAugSepOctNovDec"); make new array of Strings. For example lets call it monthArray.
monthArray[0] = "Jan";
monthArray[1] = "Feb";
...
...
monthArrat[11] = "Dec";
then, when you need to show for example 4th month you can just return monthArray[3].
- 07-13-2012, 07:53 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
Re: Month class
There are many ways to solve this problem. But the original requirement is to declare a month string and walk it using the substring method. Here's one way to do this:
The key to understanding this is lines 8-10.Java Code:public static String getMonthName(int i) { if ((i < 1) || (i > 12)) // user must enter 1 - 12 { System.err.println("error - invalid month index: " + i); return ""; } String monthName = "January February March April May June July August SeptemberOctober November December "; int startIndex = (i-1)*9; int endIndex = startIndex + 9; return monthName.substring(startIndex, endIndex); }
- 07-14-2012, 05:22 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Re: Month class
thanks, but I haven't study arrays yet...
- 07-14-2012, 05:23 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Re: Month class
Thanks, but I haven't study decisions yet, have to use hint for exercise...
- 07-14-2012, 05:58 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Month class
Enums? Or haven't you studied them either? Using enums you don't need arrays of Strings nor any hanky panky with single Strings.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-14-2012, 06:40 PM #10
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
[Req] a timer that Day Month Year
By AhrimanSefid in forum New To JavaReplies: 5Last Post: 04-13-2011, 10:17 AM -
Verifying of Month
By ŖΫ ỏ Ңόρę in forum New To JavaReplies: 2Last Post: 11-02-2010, 04:34 PM -
get the value from a table in a month range
By javastuden in forum JDBCReplies: 1Last Post: 02-24-2010, 07:53 AM -
Current month
By Java Tip in forum Java TipReplies: 0Last Post: 01-07-2008, 08:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks