Results 1 to 14 of 14
  1. #1
    karen.tao is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Red face adding days to a date

    I want to add a day to a constant start date whenever I choose to start a new day in my program
    This is what I have so far:

    GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
    SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");
    public void setStart()
    {
    startDate.setLenient(false);
    System.out.println(sdf.format(startDate.getTime()) );
    }

    public int today()
    {
    newDay = startDate.add(5, 1);
    System.out.println(newDay);
    }

    I keep on getting the error found void but expected int. How do I fix this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Welcome to the forum.

    Your question is a little hard to answer as we must guess important parts of your code (what is newDay for example?), but I think that the key here is the add method of Calendar. If you look at the API for Calendar and GregorianCalendar (and you should do this), you'll see what add does and what add "returns", and it will show that you're using it wrong. Much luck.

    edit, for e.g.,
    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    
    public class Fu2 {
    
      public static void main(String[] args) {
        GregorianCalendar startDate = 
          new GregorianCalendar(2009, Calendar.JANUARY, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");
        startDate.setLenient(false);
        System.out.println(sdf.format(startDate.getTime()));
    
        System.out.println("Let's add 15 days to the date, 10 times");
        for (int i = 0; i < 10; i++) {
          startDate.add(Calendar.DAY_OF_YEAR, 15);
          System.out.println(sdf.format(startDate.getTime()));
        }
        
        System.out.println();
        System.out.println("Let's re-initizize the date and add one month to the date, 10 times");
        startDate = 
          new GregorianCalendar(2009, Calendar.JANUARY, 1);
        for (int i = 0; i < 10; i++) {
          startDate.add(Calendar.MONTH, 1);
          System.out.println(sdf.format(startDate.getTime()));
        }
    
      }
    
    }
    Last edited by Fubarable; 09-13-2009 at 02:14 PM.

  3. #3
    karen.tao is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    import java.text.*;
    
    public class Prison
    {
        private int newDay;
        GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy"); 
        
        public Prison()
        {
        }
        
        public void setStart()
        {
            startDate.setLenient(false);
            System.out.println(sdf.format(startDate.getTime()));
        }
        
        public void today()
        {
            startDate.add(Calendar.DAY_OF_MONTH, 1);
            System.out.println(sdf.format(startDate.getTime()));
        }
    }
    So far I have this. So when I decide to start a new day, I will call the today() method, but how do I make it so that if I choose to start a new day again, it will add a day not to the startDate, but add another day?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your question is not very clear to me. Could you please elaborate a bit?

    Also your code is a bit confusing. A method name starting "set" is usually used to set a property of the class. For instance, if I had a method called setStart, I would expect it to have a parameter that would be used to set a "start" field held by the object. Also, a method called today() would in my mind reset a date to today's date.
    Last edited by Fubarable; 09-13-2009 at 02:41 PM.

  5. #5
    karen.tao is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    I want to make it so that when I run that method again, it won't just print out the same thing (2/1/09) because the startDate is 1/1/09. I want it to print out 3/1/09 on the 2nd time I run that method, 4/1/09 on the 3rd and so on. The way I did it, I think it will only keep printing out 2/1/09.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Better you should tell us what the overall goal of this program is, rather than the current specific requirements. There may be a better way to do things then the current implementation. Also, you may wish to separate your increment methods from your display methods.

  7. #7
    karen.tao is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    I am creating a prison system which sets a constant start date 1/1/09 and I need to prompt the user to enter actions such as n/a/e/r/d/p/c/s/?/x. Right now I am working on d which is to create a new day and print out the new date. After that my program needs to print out the prisoners to be released on that day.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Sorry to keep asking questions here, but it really does help me to figure out what is going on. So on that note,...

    What does this start date represent? Is the user supposed to create a new day each day? In other words are you trying to use this method to keep track of time? Is the startDate supposed to remain invariant (unchanged) throughout the program? What do these other actions represent: n/a/e/r/d/p/c/s/?/x?

  9. #9
    karen.tao is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    n - create a prisoner - must enter name, crime and days sentenced and prisoner ID will be shown.
    a - allocate a cell - must enter prisoner ID, and cell allocation will be displayed.
    e - extend a sentence - enter prisoner ID, new crime committed and days sentenced will be displayed.
    r - reduce a sentence - enter prisoner ID and will reduce a stay by one day
    d - new day - shows the next day and details of the prisoners who will be released that day
    p - prisoner details - shows id, name, start and end date of sentence and under that will show the start and end dates of individual crimes committed
    c - shows cell allocation - shows cells of each prisoner along with their ID
    s - shows statistics - calculates the average sentence and most common crime
    ? - describes the choices
    x - exits system

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hm, how about something on this order:
    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    
    public class CalendarFun {
      private static final SimpleDateFormat SDF = new SimpleDateFormat("d/M/yyyy");
      private final GregorianCalendar startDay = new GregorianCalendar(2009, Calendar.JANUARY, 1);
      private GregorianCalendar newDay;
      
      public CalendarFun() {
        newDay = (GregorianCalendar) startDay.clone();
      }
      
      public void increment1Day() {
        newDay.add(Calendar.DAY_OF_YEAR, 1);
      }
      
      public String getNewDayString() {
        return SDF.format(newDay.getTime());
      }
      
      public static void main(String[] args) {
        CalendarFun cf = new CalendarFun();
        System.out.println(cf.getNewDayString());
        cf.increment1Day();
        System.out.println(cf.getNewDayString());
      }
      
    }
    It again separates out the incrementing of the date and the getting a string for display.

  11. #11
    karen.tao is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    what does newDay = (GregorianCalendar) startDay.clone(); do?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It makes a copy of the startDay and places it in newDay. This way you could advance newDay while startDay remains unchanged. Another way to do this would be to simply construct newDay with the same constructor as startDay.

  13. #13
    karen.tao is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    awesome thanks! I have another question..

    Java Code:
    import java.util.*;
    public class Prisoner 
    {
        private String name;
        private int id;
        private int start;
        private String crimeName;
        private Cells cells;
        private Prison prison = new Prison();
        private CrimeType crime;
        private LinkedList<Prisoner> prisoners = new LinkedList<Prisoner>();
    
        public Prisoner()
        {
            //this.name = name;
            //this.id = id;  
            use();
        }
        
        public char enterAction()
        {
            System.out.print("\tEnter action (N/A/E/R/D/P/C/S/?/X): ");
            return In.nextLine().toUpperCase().charAt(0);
        }
        
        public void use()
        {   
            char action = enterAction();
            execute(action);
            action = enterAction();  
        }
        
        public void execute(char action)
        {
            switch(action)
            {
                case 'N': add(); break;
                case 'A': allocate(); break;
                case 'E': extend(); break;
                case 'R': reduce(); break;
                case 'D': newDay(); break;
                case 'P': prisoners(); break;
                case 'C': cells(); break;
                case 'S': stats(); break;
                case '?': explainMenu(); break;
                case 'X': exit(); break;
                default: System.out.println("\t\t\tThat was not a valid choice.");
                         System.out.println("\t\t\tEnter a new action (? for help).");
                         enterAction(); 
            }
        }
        
        public void add()
        {
            System.out.println("\tCreating criminal");
            enterName();
            enterCrime();
            totalSentence();
            enterAction();
            prisoners.add(new Prisoner());
        }
        
        public void allocate()
        {
            System.out.println("\tAllocate a cell");
            prisonerId();       
            allocation();
            enterAction();       
        }
        
        public void extend()
        {
            System.out.println("\t\tExtending a stay");
            prisonerId();
            enterCrime();
            totalSentence();
            enterAction();
        }
        
        public void reduce()
        {
            System.out.println("\t\tReducing a stay");
            prisonerId();
            totalSentence();
            enterAction();
        }
        
        public void newDay()
        {
            System.out.print("\t\tNew day ");
            prison.incrementDay();
            enterAction();
        }
        
        public void prisoners()
        {
            System.out.println("The prisoners are");
            System.out.print("\t" + id + name + "Start ");
            prison.startSentence();
            System.out.print(", end");
            prison.endSentence();
            System.out.print("\n\t\t" + crimeName);
            //crimeName;
            System.out.print(": start ");
            prison.startSentence();
            System.out.print(" , end ");
            prison.endSentence();
            System.out.print(" period ");
            prison.period();
            enterAction();
        }
        
        public void cells()
        {
            System.out.println("\tCell allocation is ");
            enterAction();
        }
        
        public void stats()
        {
            enterAction();
        }
        
        public void explainMenu()
        {
            System.out.println("\tThe choices are");
            System.out.println("\t\tN: Create a prisoner");
            System.out.println("\t\tA: Allocate a cell");
            System.out.println("\t\tE: Extend a sentence");
            System.out.println("\t\tR: Reduce a sentence");
            System.out.println("\t\tD: Start a new day");
            System.out.println("\t\tP: Show prisoners");
            System.out.println("\t\tC: Show cell allocation");
            System.out.println("\t\tS: Show statistics");
            System.out.println("\t\t?: Explain the choices");
            System.out.println("\t\tX: Exit the system");
            enterAction();
        }
        
        public void exit()
        {
            System.out.println("Exiting the prison system");
        }
        
        public int prisonerId()
        {
            System.out.print("\t\tPrisoner id (0 to exit): ");
            return id = In.nextInt(); 
            //if ("0".equals(id))
                //System.out.println("");
        }
        
        public void allocation()
        {
            System.out.print("\t\t" + name + " is in the");
            cells.bunk();
            System.out.print(" bunk of cell ");
            cells.number();
        }
        
        public String enterName()
        {
            System.out.print("\t\tEnter person's name: ");
            return name = In.nextLine(); 
        }
        
        public void enterCrime()
        {
            Crimes crimes = new Crimes();
            System.out.print("\t\tEnter crime: ");
            crimeName = In.nextLine();
            
            try 
            {
                if("murder".equals(crimeName) || "arson".equals(crimeName) || "assault".equals(crimeName))
                {            
                    crimes.daysToStay(3);        
                }
                else if("fraud".equals(crimeName) || "theft".equals(crimeName) || "vandalism".equals(crimeName))
                {
                    crimes.daysToStay(2);
                }
                else if("drunk".equals(crimeName) || "littering".equals(crimeName) || "badHair".equals(crimeName))
                {
                    crimes.daysToStay(1);
                }
                else 
                {
                    //System.out.println("\t\tThat is not a valid crime. The crimes are");
                    //crimes.list();
                }
            }
                catch (IllegalArgumentException e)
                {
                    System.out.println("That is not a valid crime. The crimes are");
                    crimes.list();
                }
               
            crimes.add(crimeName);
            enterAction();  
        }
        
        public void myCrime()
        {
            //list out a specific criminal's crime
        }
        
        public void totalSentence()
        {
            System.out.println("\t\t\tDays: " + prison.daysSentenced());
            System.out.println("\t\t\tMonths: " + prison.monthsSentenced());
            System.out.println("\t\t\tYears: " + prison.yearsSentenced());
        }
        
        public void startSentence()
        {
            //return start = prison.today();
        }
        
        public void release()
        {}
    }
    When I run this through use(); method in the constructor, the program runs very strangely:

    Enter action (N/A/E/R/D/P/C/S/?/X): n
    Creating criminal
    Enter person's name: karen
    Enter crime: drunk
    Enter action (N/A/E/R/D/P/C/S/?/X): ?
    Days: 1
    Months: 1
    Years: 1
    Enter action (N/A/E/R/D/P/C/S/?/X): x
    Enter action (N/A/E/R/D/P/C/S/?/X): x
    Exiting the prison system
    Enter action (N/A/E/R/D/P/C/S/?/X): ?
    Enter action (N/A/E/R/D/P/C/S/?/X): ?

    That happens in the terminal and it doesn't stop prompting when I type in x, and it stops prompting when I typed in that last ?. When I run the methods manually, all the methods run as I have typed in my code. Why is it like this and how can I fix it?

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

Similar Threads

  1. Replies: 8
    Last Post: 01-06-2011, 06:33 PM
  2. Replies: 3
    Last Post: 08-11-2009, 12:18 AM
  3. Replies: 7
    Last Post: 07-21-2009, 02:39 AM
  4. I want to fetch last 7 days records in java
    By rasikow in forum New To Java
    Replies: 3
    Last Post: 12-05-2008, 12:34 PM
  5. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 09:06 AM

Tags for this Thread

Posting Permissions

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