d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 100 Fg For Super Easy Help . Simple Java Loop > Need Help In The Next Hour And 14 Mins
Add Reply New Topic New Poll
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Apr 8 2013 06:46am
okay so my method is EXTREMELY cluttered , far from eloquent etc...


my issue is the method worked fine prior and now it does not..




my output is asking for days (ie from 1 to 1000) to add to the date jan 01 2000

and should convert days into months, days, and years. now my output is keeping the month the same and only adding days. i have a subtraction method and that works fine




heres the method




//adds days
public void addDays (int amt)
{
System.out.println("enter days to be subtracted or added from JAN-01-2000");
amt = keyboard.nextInt();



{

if (amt > 0)
{
monthChars = "JAN";
currentDay = amt;
}
else if (amt > 59)
{
monthChars = "FEB";
currentDay = amt%31;
}

else if (amt > 90)
{
monthChars = "MAR";
currentDay = amt%59;
}

else if (amt > 120)
{
monthChars = "APR";
currentDay = amt%90;
}

else if (amt > 151)
{
monthChars = "MAY";
currentDay = amt%120;
}

else if (amt > 181)
{
monthChars = "JUN";
currentDay = amt%151;
}

else if (amt > 212)
{
monthChars = "JUL";
currentDay = amt%181;
}

else if (amt > 243)
{
monthChars = "AUG";
currentDay = amt%212;
}

else if (amt > 273)
{
monthChars = "SEP";
currentDay = amt%243;
}

else if (amt > 304)
{
monthChars = "OCT";
currentDay = amt%273;
}

else if (amt > 334)
{
monthChars = "NOV";
currentDay = amt%273;
}

else if (amt > 364)
{
monthChars = "DEC";
currentDay = amt%334;
}

if (amt > 365)
{
oldYear = oldYear +(amt/365);
amt = amt % 365;
}
// subtraction

else if (amt < 0)
{
oldYear --;
oldYear = oldYear +(amt/365);
amt = amt%365;
monthChars = "DEC";
currentDay = 31 + amt;
}


else if (amt < -31)
{
monthChars = "NOV";
placeHolder= amt%31;
currentDay = 30 + placeHolder;
}
else if (amt < -61)
{
monthChars = "OCT";
placeHolder = amt%61;
currentDay = 31 + placeHolder;
}

else if (amt < -92)
{
monthChars = "SEP";
placeHolder = amt%92;
currentDay = 30 + placeHolder;
}

else if (amt < -122)
{
monthChars = "AUG";
placeHolder = amt%122;
currentDay = 31 + placeHolder;
}

else if (amt < -153)
{
monthChars = "JUL";
placeHolder = amt%153;
currentDay = 31 + placeHolder;
}

else if (amt < -184)
{
monthChars = "JUN";
placeHolder = amt%184;
currentDay = 30 + placeHolder;
}

else if (amt < -214)
{
monthChars = "MAY";
placeHolder = amt%214;
currentDay = 31 + placeHolder;
}

else if (amt < -245)
{
monthChars = "APR";
placeHolder = amt%245;
currentDay = 30 + placeHolder;
}

else if (amt < -275)
{
monthChars = "MAR";
placeHolder = amt%275;
currentDay = 31 + placeHolder;
}

else if (amt < -306)
{
monthChars = "FEB";
placeHolder = amt%306;
currentDay = 28 + placeHolder;
}

else if (amt < -334)
{
monthChars = "JAN";
placeHolder = amt%334;
currentDay = 31 + placeHolder;
}

else if ((amt < -364) && (amt >= -365))
{
monthChars = "JAN";
placeHolder = amt%334;
currentDay = 31 + placeHolder;
}

d1 = monthChars + "-" + currentDay + "-" + oldYear;
System.out.println(d1);

}

}


and heres my instances


public static final int JAN = 1;
public static final int FEB = 2;
public static final int MAR = 3;
public static final int APR = 4;
public static final int MAY = 5;
public static final int JUN = 6;
public static final int JUL = 7;
public static final int AUG = 8;
public static final int SEP = 9;
public static final int OCT = 10;
public static final int NOV = 11;
public static final int DEC = 12;
public static final String oldDate = "JAN-01-2000";

private int currentDate;
private int currentMonth;
private int currentDay;
private int currentYear;


private int oldDay = 01 ;
private int oldYear = 2000;

private int currentMonthCopy;
private int currentDayCopy;
private int currentYearCopy;

public int month;
public int day;
public int year;
public static int m;
public int d;
public int y;
public String monthHolder;
public String monthChars;
public static String d1;
public String result;
public int months;
public boolean isEqual;
public int dayNumber;
public static int amt;
public static int placeHolder;
public static int daysApart;
public static int yearHolder;


heres an example of my output for AMT entered as 200


JAN-200-2000



heres an example of my output for AMT entered as -200

DEC--169-1999
DEC--169-1999 precedes JAN-01-2000

Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Apr 8 2013 06:50am
Why did you change it if it worked?!
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Apr 8 2013 06:54am
i was just coding away. this is an assignment that i forgot was due today . it worked when i had this section completed, now that ive finished other sections such as subtraction its giving me this issue. ive reread it several times
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Apr 8 2013 06:56am
this is my full haggard code if it helps debug


Code



  import java.util.Scanner;
  public class DateTester
  {

public static final int JAN = 1;
public static final int FEB = 2;
public static final int MAR = 3;
public static final int APR = 4;
public static final int MAY = 5;
public static final int JUN = 6;
public static final int JUL = 7;
public static final int AUG = 8;
public static final int SEP = 9;
public static final int OCT = 10;
public static final int NOV = 11;
public static final int DEC = 12;
public static final String oldDate = "JAN-01-2000";

private int currentDate;
private int currentMonth;
private int currentDay;
private int currentYear;


private int oldDay = 01;
private int oldYear = 2000;

private int currentMonthCopy;
private int currentDayCopy;
private int currentYearCopy;

public int month;
public int day;
public int year;
public static int m;
public int d;
public int y;
public String monthHolder;
public String monthChars;
public static String d1;
public String result;
public int months;
public boolean isEqual;
public int dayNumber;
public static int amt;
public static int placeHolder;
public static int daysApart;
public static int yearHolder;

     Scanner keyboard = new Scanner(System.in);
 
 /* main method, creates new hangmang class, runs set date methods **/
     public static void main( String[] args)
     {
        DateTester initialDate = new DateTester();
  System.out.println(" enter a date in format of  MM  DD YYYY.");
        initialDate.setDate();
  initialDate.getCurrentDate();
    initialDate.toString();  
  System.out.println(" the day number is .");
  initialDate.getDayNumber(m);
  initialDate.addDays(amt);
  initialDate.compareTo(d1);
     }
 
 
  //default constructor
public DateTester()
{
 currentMonth = JAN;
 currentDay = 1;
 currentYear = 2000;
}
 
// constructor taking three parameters, day month year
public DateTester (int month, int day, int year)
{
 currentMonth =  month;
 currentDay = day;
 currentYear = year;
 
}
 
// copy constructor FOR MONTH in this case
public int DateTesterCopy (DateTester d)
{
 currentMonthCopy =  month;
 currentDayCopy = day;
 currentYearCopy = year;
 return currentMonthCopy;
}
 


//accessor methods for each
public int getMonth()
{
 return currentMonth;
}

 public int getDay()
{
 return currentDay;
}

 public int getYear()
{
 return currentYear;
}

 public void getCurrentDate()
{
 System.out.println(currentMonth + "-" + currentDay + "-" + currentYear);
}


// mutator methods for each

 public void setMonth (int m)
  {
  System.out.println(" enter month as integer ie jan = 01 :");
  //monthHolder = keyboard.next();
  //m = months;
  m = keyboard.nextInt();
  /* if ((m < 1) || (m >12))
  {
  throw new Exception("please enter a valid month, between 1 and 12" );
  }
  else
  {  
  currentMonth = m;
  }
  **/
  currentMonth = m;
  }
 
 public void setDay (int d)
  {
  System.out.println(" enter day as integer, ie for 15th of jan, enter 15:");
  d = keyboard.nextInt();
  currentDay = d;
  }
 
 public void setYear (int y)
  {
  System.out.println(" enter year as integer, ie 2002:");
  y = keyboard.nextInt();
  currentYear = y;
  }
 
 
  // skipped using enum methods
 /*public enum Months
  {
     JANUARY ,
  FEBRUARY ,
  MARCH ,
  APRIL ,
  MAY ,
  JUNE ,
  JULY ,
  AUGUST ,
  SEPTEMBER ,
  OCTOBER ,
  NOVEMBER ,
  DECEMBER ,
    }
     
 public Months ToInt(Months Input)
  {
    return (int)Enum.Parse(typeof(Months), Input, true);
  }  
 **/
 
 public void setDate ()
  {
  setMonth(m);
  setDay(d);
  setYear(y);
  System.out.print(" the current date is set to ");
  }
 
 
 
 /*
 String result = formatDate( monthChars, currentDay, currentYear);
  public  String formatDate( String monthChars, int currentDay, int currentYear )
 {
   {
     return String.format("testing code here %s!", monthChars, currentDay, currentYear);
     }
 
 }
 **/
 
 public String toString()
  {
   do
   {
   switch(currentMonth)
    {
    case 01: monthChars = "JAN";
    break;
    case 02: monthChars = "FEB";
    break;
    case 03: monthChars = "MAR";
    break;
    case 04: monthChars = "APR";
    break;
    case 05: monthChars = "MAY";
    break;
    case 06: monthChars = "JUN";
    break;
    case 07: monthChars = "JUL";
    break;
    case 8: monthChars =  "AUG";
    break;
    case 9: monthChars = "SEP";
    break;
    case 10: monthChars = "OCT";
    break;
    case 11: monthChars = "NOV";
    break;
    case 12: monthChars = "DEC";
    break;
    default:
    System.out.println("invalid month specified");
    }
   } while (currentMonth >12);
   
   d1 = monthChars + "-" + currentDay + "-" + currentYear;
  System.out.println(" my date is " + d1);
  return d1;
  }
  // same check is used in compare to;
 /* public boolean equals (String d1)
  {
   if (oldDate.compareTo(d1)== 0)
   {
    return true;
   }
  else
   {
   return false;
   }
  }
 
  **/
 
 public int getDayNumber(int m)
  {
     
   switch(currentMonth)
    {
    case 01: dayNumber = 0 + currentDay;
    break;
    case 02: dayNumber = 31 + currentDay;
    break;
    case 03: dayNumber = 59 + currentDay;
    break;
    case 04: dayNumber = 90 + currentDay;
    break;
    case 05: dayNumber = 120 + currentDay;
    break;
    case 06: dayNumber = 151 + currentDay;
    break;
    case 07: dayNumber = 181 + currentDay;
    break;
    case 8:  dayNumber =  212 + currentDay;
    break;
    case 9: dayNumber = 243 + currentDay;
    break;
    case 10: dayNumber = 273 + currentDay;
    break;
    case 11: dayNumber = 304 + currentDay;
    break;
    case 12: dayNumber = 334 + currentDay;
    break;
    default:
    System.out.println("invalid month specified");

    }
   
   System.out.println(dayNumber);
   getDaysApart(dayNumber);
   return dayNumber;
  }
 
 
 
 
 
  //adds days
  public void addDays (int amt)
  {
   System.out.println("enter days to be subtracted or added from JAN-01-2000");
   amt = keyboard.nextInt();
   
 
   
  {
   
   if (amt > 0)
   {
   monthChars = "JAN";
   currentDay = amt;
   }
   else if (amt > 59)
   {
   monthChars = "FEB";
   currentDay = amt%31;
   }
     
   else if (amt > 90)
   {
   monthChars = "MAR";
   currentDay = amt%59;
   }
   
   else if (amt > 120)
   {
   monthChars = "APR";
   currentDay = amt%90;
   }
   
   else if (amt > 151)
   {
   monthChars = "MAY";
   currentDay = amt%120;
   }
   
   else if (amt > 181)
   {
   monthChars = "JUN";
   currentDay = amt%151;
   }
   
   else if (amt > 212)
   {
   monthChars = "JUL";
   currentDay = amt%181;
   }
   
   else if (amt > 243)
   {
   monthChars = "AUG";
   currentDay = amt%212;
   }
   
   else if (amt > 273)
   {
   monthChars = "SEP";
   currentDay = amt%243;
   }
   
   else if (amt > 304)
   {
   monthChars = "OCT";
   currentDay = amt%273;
   }
   
   else if (amt > 334)
   {
   monthChars = "NOV";
   currentDay = amt%273;
   }
   
   else if (amt > 364)
   {
   monthChars = "DEC";
   currentDay = amt%334;
   }
   
   if (amt > 365)
   {
   oldYear = oldYear +(amt/365);
   amt = amt % 365;
   }
   // subtraction
   
   else if  (amt < 0)
   {
   oldYear --;
   oldYear = oldYear +(amt/365);
   amt = amt%365;
   monthChars = "DEC";
   currentDay = 31 + amt;
   }
   
   
   else if (amt < -31)
   {
   monthChars = "NOV";
   placeHolder= amt%31;
   currentDay = 30 + placeHolder;
   }
   else if (amt < -61)
   {
   monthChars = "OCT";
   placeHolder = amt%61;
   currentDay = 31 + placeHolder;
   }
     
   else if (amt < -92)
   {
   monthChars = "SEP";
   placeHolder = amt%92;
   currentDay = 30 + placeHolder;
   }
   
   else if (amt < -122)
   {
   monthChars = "AUG";
   placeHolder = amt%122;
   currentDay = 31 + placeHolder;
   }
   
   else if (amt < -153)
   {
   monthChars = "JUL";
   placeHolder = amt%153;
   currentDay = 31 + placeHolder;
   }
   
   else if (amt < -184)
   {
   monthChars = "JUN";
   placeHolder = amt%184;
   currentDay = 30 + placeHolder;
   }
   
   else if (amt < -214)
   {
   monthChars = "MAY";
   placeHolder = amt%214;
   currentDay = 31 + placeHolder;
   }
   
   else if (amt < -245)
   {
   monthChars = "APR";
   placeHolder = amt%245;
   currentDay = 30 + placeHolder;
   }
   
   else if (amt < -275)
   {
   monthChars = "MAR";
   placeHolder = amt%275;
   currentDay = 31 + placeHolder;
   }
   
   else if (amt < -306)
   {
   monthChars = "FEB";
   placeHolder = amt%306;
   currentDay = 28 + placeHolder;
   }
   
   else if (amt < -334)
   {
   monthChars = "JAN";
   placeHolder = amt%334;
   currentDay = 31 + placeHolder;
   }
   
   else if ((amt < -364) && (amt >= -365))
   {
   monthChars = "JAN";
   placeHolder = amt%334;
   currentDay = 31 + placeHolder;
   }

   d1 = monthChars + "-" + currentDay + "-" + oldYear;
   System.out.println(d1);
   
   }
 
  }
  // tells which date comes first
  public int compareTo (String d1)
  {
  if (d1.compareTo(oldDate)<0)
   {
   System.out.println(d1 + " precedes " + oldDate);
   }
  else if (d1.compareTo(oldDate)>0)
   {
   System.out.println(d1 + " comes after " + oldDate);
   }
  else if (d1.compareTo(oldDate)==0)
  {
  System.out.println(d1 + " is the same as " + oldDate);
  }
  return 0;
  }
  // tells how many days apart original date and inputed date are
  public int getDaysApart (int dayNumber)
  {
  daysApart = 0 + dayNumber;
  yearHolder = oldYear - currentYear;
  daysApart = daysApart + ((Math.abs(yearHolder))*365);
  System.out.println( " the new date " + d1 + " and the old date " +  oldDate + " are " + daysApart + " days apart ");
  return daysApart;
  }
 
 }
 
 
 

Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 8 2013 07:07am
The problem is in the first if statment. It will always be true if the number is greater than 0, so the rest of the if statements will be ignored. Change

Code
if (amt > 0)
  {
  monthChars = "JAN";
  currentDay = amt;
  }


to

Code

     if ( amt >= 0 && amt < 59 ) {
       monthChars = "JAN";
       currentDay = amt;
     }


And do the same for the rest of the if statements
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Apr 8 2013 07:13am
Quote (labatymo @ Apr 8 2013 06:07am)
The problem is in the first if statment. It will always be true if the number is greater than 0, so the rest of the if statements will be ignored. Change

Code
if (amt > 0)
  {
  monthChars = "JAN";
  currentDay = amt;
  }


to

Code
if ( amt >= 0 && amt < 59 ) {
       monthChars = "JAN";
       currentDay = amt;
     }


And do the same for the rest of the if statements



holy shit. i guess i mustve erased it sometime, but i figured they override each time. either way youre a life saver
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Apr 8 2013 01:25pm
It would only 'override' if you just used "IF" statements and not else if
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll