1 2 Previous Next 15 Replies Latest reply: Oct 26, 2007 4:07 AM by masijade RSS

    Replacing String month with type Int - What to do!?!

    807600
      import java.util.Scanner;
      
      public class Date
      {
          private String month;
          private int day;
          private int year; //a four digit number.
      
          public Date( )
          {
              month = "January";
              day = 1;
              year = 1000;
          }
      
          public Date(int monthInt, int day, int year)
          {
              setDate(monthInt, day, year);
          }
      
          public Date(String monthString, int day, int year)
          {
              setDate(monthString, day, year);
          }
      
          public Date(int year)
          {
              setDate(1, 1, year);
          }
      
          public Date(Date aDate)
          {
              if (aDate == null)//Not a real date.
              {
                   System.out.println("Fatal Error.");
                   System.exit(0);
              }
      
              month = aDate.month;
              day = aDate.day;
              year = aDate.year;
          }
      
          public void setDate(int monthInt, int day, int year)
          {
              if (dateOK(monthInt, day, year))
              {
                  this.month = monthString(monthInt);
                  this.day = day;
                  this.year = year;
              }
              else
              {
                  System.out.println("Fatal Error");
                  System.exit(0);
              }
          }
      
          public void setDate(String monthString, int day, int year)
          {
              if (dateOK(monthString, day, year))
              {
                  this.month = monthString;
                  this.day = day;
                  this.year = year;
              }
              else
              {
                  System.out.println("Fatal Error");
                  System.exit(0);
              }
          }
      
          public void setDate(int year)
          {
              setDate(1, 1, year);
          }
      
          public void setYear(int year)
          {
              if ( (year < 1000) || (year > 9999) )
              {
                  System.out.println("Fatal Error");
                  System.exit(0);
              }
              else
                  this.year = year;
          }
          public void setMonth(int monthNumber)
          {
              if ((monthNumber <= 0) || (monthNumber > 12))
              {
                  System.out.println("Fatal Error");
                  System.exit(0);
              }
              else
                  month = monthString(monthNumber);
          }
      
          public void setDay(int day)
          {
              if ((day <= 0) || (day > 31))
              {
                  System.out.println("Fatal Error");
                  System.exit(0);
              }
              else
                  this.day = day;
          }
      
          public int getMonth( )
          {
              if (month.equals("January"))
                  return 1;
              else if (month.equals("February"))
                  return 2;
              else if (month.equalsIgnoreCase("March"))
                  return 3;
              else if (month.equalsIgnoreCase("April"))
                  return 4;
              else if (month.equalsIgnoreCase("May"))
                  return 5;
              else if (month.equals("June"))
                  return 6;
              else if (month.equalsIgnoreCase("July"))
                  return 7;
              else if (month.equalsIgnoreCase("August"))
                  return 8;
              else if (month.equalsIgnoreCase("September"))
                  return 9;
              else if (month.equalsIgnoreCase("October"))
                  return 10;
              else if (month.equals("November"))
                  return 11;
              else if (month.equals("December"))
                  return 12;
              else
              {
                  System.out.println("Fatal Error");
                  System.exit(0);
                  return 0; //Needed to keep the compiler happy
              }
          }
      
          public int getDay( )
          {
              return day;
          }
      
          public int getYear( )
          {
              return year;
          }
      
          public String toString( )
          {
              return (month + " " + day + ", " + year);
          }
      
          public boolean equals(Date otherDate)
          {
              return ( (month.equals(otherDate.month))
                        && (day == otherDate.day) && (year == otherDate.year) );
          }
      
          public boolean precedes(Date otherDate)
          {
              return ( (year < otherDate.year) ||
                 (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
                 (year == otherDate.year && month.equals(otherDate.month)
                                               && day < otherDate.day) );
          }
      
          public void readInput( )
          {
              boolean tryAgain = true;
              Scanner keyboard = new Scanner(System.in);
              while (tryAgain)
              {
                  System.out.println("Enter month, day, and year.");
                    System.out.println("Do not use a comma.");
                  String monthInput = keyboard.next( );
                  int dayInput = keyboard.nextInt( );
                  int yearInput = keyboard.nextInt( );
                  if (dateOK(monthInput, dayInput, yearInput) )
                  {
                      setDate(monthInput, dayInput, yearInput);
                      tryAgain = false;
                  }
                  else
                      System.out.println("Illegal date. Reenter input.");
               }
          }
      
          private boolean dateOK(int monthInt, int dayInt, int yearInt)
          {
              return ( (monthInt >= 1) && (monthInt <= 12) &&
                       (dayInt >= 1) && (dayInt <= 31) &&
                       (yearInt >= 1000) && (yearInt <= 9999) );
          }
      
          private boolean dateOK(String monthString, int dayInt, int yearInt)
          {
              return ( monthOK(monthString) &&
                       (dayInt >= 1) && (dayInt <= 31) &&
                       (yearInt >= 1000) && (yearInt <= 9999) );
          }
      
          private boolean monthOK(String month)
          {
              return (month.equals("January") || month.equals("February") ||
                      month.equals("March") || month.equals("April") ||
                      month.equals("May") || month.equals("June") ||
                      month.equals("July") || month.equals("August") ||
                      month.equals("September") || month.equals("October") ||
                      month.equals("November") || month.equals("December") );
          }
      
          private String monthString(int monthNumber)
          {
              switch (monthNumber)
              {
              case 1:
                  return "January";
              case 2:
                  return "February";
              case 3:
                  return "March";
              case 4:
                  return "April";
              case 5:
                  return "May";
              case 6:
                  return "June";
              case 7:
                  return "July";
              case 8:
                  return "August";
              case 9:
                  return "September";
              case 10:
                  return "October";
              case 11:
                  return "November";
              case 12:
                  return "December";
              default:
                  System.out.println("Fatal Error");
                  System.exit(0);
                  return "Error"; //to keep the compiler happy
              }
          }
      }
      My question is that if I were to change the String month in the instance variables to type int, what changes do I make to the code to make this work WITHOUT changing the method headings and it also says that none of the type String paraenters should change to type int. I need to redefine the methods to make it work. I've been at this for hours and am just stuck so this is the reason for my early morning post.
        • 1. Re: Replacing String month with type Int - What to do!?!
          masijade
          Define an array of month names so that you can do monthnames[monthnum]
          • 2. Re: Replacing String month with type Int - What to do!?!
            800384
            Why not just use the Calendar class?
            • 3. Re: Replacing String month with type Int - What to do!?!
              807600
              It's for my Programming Class in school and have to go by the homework directions. This is what they are, pointless I know, but this is how it is, again, any help would be greatly appreciated. Thank You.
              • 4. Re: Replacing String month with type Int - What to do!?!
                masijade
                watertownjordan wrote:
                Why not just use the Calendar class?
                Because he asked a stupid question, and you know how the rest of that phrase goes, and he will probably say that he is not allowed to, anyway. They always do. ;-)
                • 5. Re: Replacing String month with type Int - What to do!?!
                  masijade
                  bkpatel7 wrote:
                  It's for my Programming Class in school and have to go by the homework directions. This is what they are, pointless I know, but this is how it is, again, any help would be greatly appreciated. Thank You.
                  And? Did you read the first reply?
                  • 6. Re: Replacing String month with type Int - What to do!?!
                    807600
                    Could you possibly start writing that out for me, since we haven't gone over arrays just yet, It's the next chapter. Or atleast a more details description and I'll figure it out.
                    • 7. Re: Replacing String month with type Int - What to do!?!
                      masijade
                      Then simply make use of that "getMonth" method you already have in that class, whenever you need to pass the month as a String.
                      • 8. Re: Replacing String month with type Int - What to do!?!
                        807600
                        I'm looking at notes and ahead in the book...So...take a stab at this...

                        int [] month = new int [12];
                        • 9. Re: Replacing String month with type Int - What to do!?!
                          masijade
                          something like that, except you will already have the month "number" and you would want to use that as an the index, what you want to get out of the array is the month "name" (which is a String), so you need an array of Strings. But that is beside the point. If you haven't covered arrays, then don't use them. As I said in the last post, make use of that "getMonth" method you already have in the class.
                          • 10. Re: Replacing String month with type Int - What to do!?!
                            807600
                            I'm sorry, I don't understand. So would I switch out the String and Int types...so that way I would have
                            public int getMonth( )
                                {
                                    if (month == 1)
                                        return "Jan...";
                                    else if (month.equals("February"))
                                        return 2;
                            But that's not possible...changing just that, I get an error for the return statement.
                            • 11. Re: Replacing String month with type Int - What to do!?!
                              807600
                              import java.util.Scanner;
                               
                              public class Date
                              {
                                  private int month;
                                  private int day;
                                  private int year; //a four digit number.
                               
                                  public Date( )
                                  {
                                      month = 1;
                                      day = 1;
                                      year = 1000;
                                  }
                               
                                  public Date(int monthInt, int day, int year)
                                  {
                                      setDate(monthInt, day, year);
                                  }
                               
                                  public Date(String monthString, int day, int year)
                                  {
                                      setDate(monthString, day, year);
                                  }
                               
                                  public Date(int year)
                                  {
                                      setDate(1, 1, year);
                                  }
                               
                                  public Date(Date aDate)
                                  {
                                      if (aDate == null)//Not a real date.
                                      {
                                           System.out.println("Fatal Error.");
                                           System.exit(0);
                                      }
                               
                                      month = aDate.month;
                                      day = aDate.day;
                                      year = aDate.year;
                                  }
                               
                                  public void setDate(int monthInt, int day, int year)
                                  {
                                      if (dateOK(monthInt, day, year))
                                      {
                                          this.month = monthInt;
                                          this.day = day;
                                          this.year = year;
                                      }
                                      else
                                      {
                                          System.out.println("Fatal Error");
                                          System.exit(0);
                                      }
                                  }
                               
                                  public void setDate(String monthString, int day, int year)
                                  {
                                      if (dateOK(monthString, day, year))
                                      {
                                          this.month = monthStringToInt(monthString);
                                          this.day = day;
                                          this.year = year;
                                      }
                                      else
                                      {
                                          System.out.println("Fatal Error");
                                          System.exit(0);
                                      }
                                  }
                               
                                  public void setDate(int year)
                                  {
                                      setDate(1, 1, year);
                                  }
                               
                                  public void setYear(int year)
                                  {
                                      if ( (year < 1000) || (year > 9999) )
                                      {
                                          System.out.println("Fatal Error");
                                          System.exit(0);
                                      }
                                      else
                                          this.year = year;
                                  }
                                  public void setMonth(int monthNumber)
                                  {
                                      if ((monthNumber <= 0) || (monthNumber > 12))
                                      {
                                          System.out.println("Fatal Error");
                                          System.exit(0);
                                      }
                                      else
                                          month = monthNumber;
                                  }
                               
                                  public void setDay(int day)
                                  {
                                      if ((day <= 0) || (day > 31))
                                      {
                                          System.out.println("Fatal Error");
                                          System.exit(0);
                                      }
                                      else
                                          this.day = day;
                                  }
                               
                                  public int getMonth( )
                                  {
                                       return month;
                                  }
                               
                                  public int getDay( )
                                  {
                                      return day;
                                  }
                               
                                  public int getYear( )
                                  {
                                      return year;
                                  }
                               
                                  public String toString( )
                                  {
                                      return (month + " " + day + ", " + year);
                                  }
                               
                                  public boolean equals(Date otherDate)
                                  {
                                      return ( (month == otherDate.month)
                                                && (day == otherDate.day) && (year == otherDate.year) );
                                  }
                               
                                  public boolean precedes(Date otherDate)
                                  {
                                      return ( (year < otherDate.year) ||
                                         (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
                                         (year == otherDate.year && month == otherDate.month
                                                                       && day < otherDate.day) );
                                  }
                               
                                  public void readInput( )
                                  {
                                      boolean tryAgain = true;
                                      Scanner keyboard = new Scanner(System.in);
                                      while (tryAgain)
                                      {
                                          System.out.println("Enter month, day, and year.");
                                            System.out.println("Do not use a comma.");
                                          String monthInput = keyboard.next( );
                                          int dayInput = keyboard.nextInt( );
                                          int yearInput = keyboard.nextInt( );
                                          if (dateOK(monthInput, dayInput, yearInput) )
                                          {
                                              setDate(monthInput, dayInput, yearInput);
                                              tryAgain = false;
                                          }
                                          else
                                              System.out.println("Illegal date. Reenter input.");
                                       }
                                  }
                               
                                  private boolean dateOK(int monthInt, int dayInt, int yearInt)
                                  {
                                      return ( (monthInt >= 1) && (monthInt <= 12) &&
                                               (dayInt >= 1) && (dayInt <= 31) &&
                                               (yearInt >= 1000) && (yearInt <= 9999) );
                                  }
                               
                                  private boolean dateOK(String monthString, int dayInt, int yearInt)
                                  {
                                      return ( monthOK(monthString) &&
                                               (dayInt >= 1) && (dayInt <= 31) &&
                                               (yearInt >= 1000) && (yearInt <= 9999) );
                                  }
                               
                                  private boolean monthOK(String month)
                                  {
                                      return (month.equals("January") || month.equals("February") ||
                                              month.equals("March") || month.equals("April") ||
                                              month.equals("May") || month.equals("June") ||
                                              month.equals("July") || month.equals("August") ||
                                              month.equals("September") || month.equals("October") ||
                                              month.equals("November") || month.equals("December") );
                                  }
                               
                                  private String monthString(int monthNumber)
                                  {
                                      switch (monthNumber)
                                      {
                                      case 1:
                                          return "January";
                                      case 2:
                                          return "February";
                                      case 3:
                                          return "March";
                                      case 4:
                                          return "April";
                                      case 5:
                                          return "May";
                                      case 6:
                                          return "June";
                                      case 7:
                                          return "July";
                                      case 8:
                                          return "August";
                                      case 9:
                                          return "September";
                                      case 10:
                                          return "October";
                                      case 11:
                                          return "November";
                                      case 12:
                                          return "December";
                                      default:
                                          System.out.println("Fatal Error");
                                          System.exit(0);
                                          return "Error"; //to keep the compiler happy
                                      }
                                  }
                                  
                                  private int monthStringToInt(String monthString) {
                                       if (monthString.equals("January")) return 1;
                                       else if (monthString.equals("February")) return 2;
                                       else if (monthString.equals("March")) return 3;
                                       else if (monthString.equals("April")) return 4;
                                       else if (monthString.equals("May")) return 5;
                                       else if (monthString.equals("June")) return 6;
                                       else if (monthString.equals("July")) return 7;
                                       else if (monthString.equals("August")) return 8;
                                       else if (monthString.equals("September")) return 9;
                                       else if (monthString.equals("October")) return 10;
                                       else if (monthString.equals("November")) return 11;
                                       else if (monthString.equals("December")) return 12;
                                       else return -1;
                                  }
                              }
                              I've added one method: monthStringToInt to convert "January", "February" into an integer value. There are probably better ways to do this (Calendar), but this involes no other classes.
                              • 12. Re: Replacing String month with type Int - What to do!?!
                                masijade
                                bkpatel7 wrote:
                                I'm sorry, I don't understand. So would I switch out the String and Int types...so that way I would have
                                public int getMonth( )
                                {
                                if (month == 1)
                                return "Jan...";
                                else if (month.equals("February"))
                                return 2;
                                But that's not possible...changing just that, I get an error for the return statement.
                                Sorry I meant a combination of the getMonth and monthName methods. That class already has everything needed, you just need to use them in a different comnbination.
                                • 13. Re: Replacing String month with type Int - What to do!?!
                                  807600
                                  Could you please elaborate more, that way I can be on a better track to completing this problem w/o confusion. Sorry I don't want to make it seem like I want you to do my hw, I just wanna be closer to finishing it and learning at the same time.
                                  • 14. Re: Replacing String month with type Int - What to do!?!
                                    807600
                                    Thanks for your help, Jan.Gressmann. I looked over the code and it seems to make sense, hopefully it works out. Thanks agian.
                                    1 2 Previous Next