0

My code asks for the user to enter the day with an integer value ie Sunday = 0, Monday = 1, etc., then asks the user to input a number representing an offset day. From there the program finds the day corresponding to the offset number ie

Enter today's day: 0 //Sunday
Enter the number of days elapsed since today: 6
6 days from Sunday is Sunday

The problem with the above output is that since I'm using, "%6" to find the offset day, it skips one day for values 6.

Also, I can't figure a way to find the day for an entered negative offset value. ie

Enter today's day: 0 //Sunday
Enter the number of days elapsed since today: -2
-2 days from Sunday is Friday

Here's my code

import java.util.Scanner ;

public class test {

public static void main (String[] args) {

Scanner Input = new Scanner(System.in);
    System.out.print("Enter today's day: ");
        int today = Input.nextInt();

System.out.print("Enter the number of days elapsed since today: ");
    int offset = Input.nextInt ();
        int offsetDay = 0;
        if (offset >= 0)
            offsetDay = (today + offset)% 6 ; //to get remainder and make it vary from 0 - 6            
        else if (offset < 0)
            offsetDay = (today - offset)% 6 ;


String todayText = null ;
String offsetText = null;

//Converting input integers into days in text for variable today
switch (today) {
    case 0 : todayText = "Sunday" ; break;
    case 1 : todayText = "Monday"; break;
    case 2 : todayText = "Tuesday"; break;
    case 3 : todayText = "Wednesday";break;
    case 4 : todayText = "Thrusday"; break;
    case 5 : todayText = "Friday"; break;
    case 6 : todayText = "Saturday"; break;
}

//Converting input integers into days in text for variable offset
switch (offsetDay) {
    case 0 : offsetText = "Sunday" ; break;
    case 1 : offsetText = "Monday"; break;
    case 2 : offsetText = "Tuesday"; break;
    case 3 : offsetText = "Wednesday";break;
    case 4 : offsetText = "Thrusday"; break;
    case 5 : offsetText = "Friday"; break;
    case 6 : offsetText = "Saturday"; break;
}

System.out.println(offset + " days from " + todayText + " is "  + offsetText);

}

}

My last question is, how would I implement this using enumeration? Any thoughts?

Cullen
  • 23
  • 7
  • 1
    Why are you using % 6 instead of % 7? – yitzih Apr 27 '14 at 23:15
  • Whoops, just thought about this one, forgot to include the 0. Thanks! – Cullen Apr 27 '14 at 23:17
  • You can see how to use enums here: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html but it wont make much of a difference in the current code. One small suggestion would be to put the switch statement inside a method that returns a String since it is exactly the same code just used to set different variables. – yitzih Apr 27 '14 at 23:19
  • Thanks, I'll implement that. Do you have a suggestion for the negative offset values? – Cullen Apr 27 '14 at 23:21
  • Remove this ""special code" for negative values (remove if) just offsetDay = (today + offset)% 6 ; / – Marco Acierno Apr 27 '14 at 23:28
  • That wouldn't work as with his current code that would return null if today + offset equals a negative number – yitzih Apr 27 '14 at 23:32
  • @Marco I changed it to offsetDay = (today + offset)% 6 and it returned null. – Cullen Apr 27 '14 at 23:36
  • The best idea I can come up with is creating a separate method for negative number that works in the reverse (i.e. case 1: return "Saturday"; break; case 2: return "Friday"; break etc...) and use a conditional statement to determine which method gets called. – yitzih Apr 27 '14 at 23:40

2 Answers2

0

You should use %7 instead of %6 as counting from 0-6 inclusive is 7.

Jesan Fafon
  • 1,954
  • 1
  • 20
  • 29
0

Please, try this code snippet, maybe this will help you:

public class Days
{

    enum DAY
    {
        MON("Monday"),
        TUE("Tuesday"),
        WED("Wednesday"),
        THU("Thursday"),
        FRI("Friday"),
        SAT("Saturday"),
        SUN("Sunday");

        private String name;

        private DAY(String name)
        {
            this.name= name;
        }

        @Override
        public String toString()
        {
            return this.name;
        }

        public DAY add(int days)
        {
            int posMod = (this.ordinal() + days) % values().length;
            if (posMod < 0)
            {
                posMod += values().length;
            }
            return DAY.values()[posMod];
        }
    }

    public static void main(String[] args)
    {
        DAY a = DAY.values()[0];
        System.out.println(a);
        System.out.println(a.add(-2));
        System.out.println(a.add(0));
        System.out.println(a.add(-8));
    }

}
Benjamin
  • 2,786
  • 2
  • 23
  • 34
  • Why didn't you name your enums `Monday`, `Tuesday` etc? What is this `MON` stuff for? – OldCurmudgeon Apr 27 '14 at 23:29
  • @OldCurmudgeon it follows the [style guide](http://stackoverflow.com/a/3069863/289086) for Java constants. –  Apr 27 '14 at 23:32
  • @OldCurmudgeon as MichaelT stated. You can change MON to MONDAY and so on... I added days calculation as you wanted. – Benjamin Apr 27 '14 at 23:44