Results 1 to 6 of 6
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with class Month assignment please!!

    Problem: In this program you will transform numbers 1, 2, 3, 4,...12 into the corresponding month names: January, February, March,...December.

    Implement class Month whose constructor parameter is the month number. Create a method getName that returns the month name.

    Hint: Make a very long string that has the month names in it: "January February...December". In which you add spaces between the month names so that each month name is the same length. Then use the substring to extract the month you want

    I cannot use array or switch

    Create a class MonthTester that uses Scanner to get the month number, creates a month object and produces an output of month name
    This is what i have:

    Java Code:
     public class Month
       {
          private String month;
          private int i;
       
          public Month(int mnth)
          {
             mnth = i;		
          
          }
       
       
          public String getMonth()
          {
             String month = "January  February  March     April     May        June     July      August    September October   November  December  ";
             int startIndex = (i-1) * 10;
             int endIndex = startIndex + 10;
             return month.substring(startIndex, endIndex);
          }
       }
    
       import java.util.*;
    
       public class MonthTester
       {
          public static void main(String[] args)
          {
             Scanner months = new Scanner(System.in);
             int mnth;
             System.out.print("Enter the month number (1-12): ");
             mnth = months.nextInt();
             Month test = new Month(mnth);
             System.out.println("The month is: "+test.getMonth());
          }
       }
    When i run the tester this is what i get:
    Java Code:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -10
    	at java.lang.String.substring(String.java:1904)
    	at Month.getMonth(Month.java:18)
    	at MonthTester.main(MonthTester.java:12)
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    Last edited by pbrockway2; 10-17-2012 at 08:46 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with class Month assignment please!!

    Hi ndsmith20, welcome to the forums!

    I've added code tags to your post. Basically the idea is when you post code put [code] at the start of the code and [/code] at the end: that way formatting is preserved by the forum software and the code is readable. There are a number of other markup tags you can use documented at BB Code List - Java Programming Forum

    Incidental to your actual question, but related to formatting... If you have a really long line of code it is common to break it up, often using twice the normal amount of indentation. So:

    Java Code:
    public String getMonth()
    {
        String month = "January  February  March     April     "
                + "May        June     July      August    "
                + "September October   November  December  ";
        int startIndex = (i-1) * 10;
        int endIndex = startIndex + 10;
        return month.substring(startIndex, endIndex);
    }
    [Edit] Now I see the month string formatted like that I can see a little bug that creeps in at May.
    Last edited by pbrockway2; 10-17-2012 at 08:57 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with class Month assignment please!!

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -10
    at java.lang.String.substring(String.java:1904)
    at Month.getMonth(Month.java:18)
    at MonthTester.main(MonthTester.java:12)
    You read these stack traces down from the top until you hit the first line of your code: that's often a good place to begin bug hunting. In this it's line 18 of Month.java. Now, I'm guessing that's

    Java Code:
    return month.substring(startIndex, endIndex);
    So it would appear you are sending some values to substring() that are invalid. One way to see what those values are is to print them:

    Java Code:
    public String getMonth()
    {
        String month = // very long string elided
        int startIndex = (i-1) * 10;
        int endIndex = startIndex + 10;
        System.out.println("About to call substring()");
        System.out.println("startIndex=" + startIndex + " endIndex=" + endIndex);
        return month.substring(startIndex, endIndex);
    }
    If that confirms that one or other of startIndex and/or endIndex are not what you intend them to be then suspicion falls on i which was used on the previous two lines to set their values. Go back to your code to see where you attempted to give i a reasonable value and think about why that didn't happen.

  4. #4
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with class Month assignment please!!

    I did what you said using a 1 as my input and received a startIndex of -10 and endIndex of 0...I dont under stand how it is coming up with these figures. The math just does not make sense

    i = 1
    (1-1) * 10 = 0

    Please help

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with class Month assignment please!!

    Have a good look at that assignment in line #8 of your code; you're assigning the value of i to parameter mnth instead of the other way around.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with class Month assignment please!!

    Changed it so the initial Month class reads i = mnth; now everything is working, thank you so much!!!

Similar Threads

  1. Month class
    By FOX427 in forum New To Java
    Replies: 9
    Last Post: 07-14-2012, 06:40 PM
  2. Help needed with class assignment.
    By DigDug in forum New To Java
    Replies: 9
    Last Post: 01-24-2012, 04:52 AM
  3. Need help with Instrument Class assignment
    By Kinney.j in forum New To Java
    Replies: 0
    Last Post: 11-02-2011, 04:42 AM
  4. Replies: 1
    Last Post: 10-02-2009, 01:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •