tavalendo
tavalendo

Reputation: 877

Working with week days and finding future days

I am trying to find means to compute future weekdays, given two inputs:

  1. Current weekday (range from 0-6 where 0 is Sunday).
  2. How many counts to perform from current weekday (any number)?
  3. Here if the user previously starts from current weekday = 3, and counts=7.
  4. Then I expect it to come back to 3, similarly with 14 or 21.
  5. How to generalize this to make the counts within this fixed range 0-6 without pulling out of it?

I've already done some code which is posted below,

public class ThoseDays {

    public static void main(String[] args) {

        Scanner obj = new Scanner(System.in);
        System.out.print("Enter number between 0-6 : ");
        int startFromHere = obj.nextInt();

        System.out.print("Enter number to count position from " + startFromHere + " : ");
        int rotateFromHere = obj.nextInt(); 


        System.out.print( startFromHere +  rotateFromHere);

        obj.close();
    }
}

Actual result:

> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 10

Expected result:

> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 3

Upvotes: 1

Views: 180

Answers (2)

govind agrawal
govind agrawal

Reputation: 21

code:

import java.util.*;
public class ThoseDays {

    public static void main(String[] args) {

        Scanner obj = new Scanner(System.in);
    System.out.println("note : 0:sun 1-6:mon-saturday");
        System.out.println("Enter the number between 0-6: ");// 0:sun 1-6:mon-saturday

        int startFromHere = obj.nextInt();

        System.out.println("Enter the number to count position from " + startFromHere+ ": ");
        int rotateFromHere =obj.nextInt();
        if(rotateFromHere%7==0)
        {
            System.out.println(startFromHere);
        }
        if(rotateFromHere%7!=0)
        {
       int dayOfWeek=(startFromHere+(rotateFromHere%7));
       if(dayOfWeek>=7)
       {
           System.out.println((dayOfWeek%7));
       }
       else
       {
           System.out.print(dayOfWeek);
       }
        }


        obj.close();
    }
}

try this code by changing conditions and using modulo I'm getting all correct results

output:
startFromHere = 3
rotate fromHere = 7 or 14 or 21 or multiple of 7
gives the same date as the start date

if rotate date is > start date
for ex:

startFromHere = 3 //wednesday
rotateFromHere = 11

output will be : 0 which means sunday

check this code and give me a rating if useful thanks.

Upvotes: 2

Stephan Hogenboom
Stephan Hogenboom

Reputation: 1573

Hi i suggest you just use a modulo to rotate the days after they reach 7. Another tutorial here

public class ThoseDays {

  public static void main(String[] args) {

  //Scanner implements AutoCloseable
  //https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
    try (Scanner obj = new Scanner(System.in)) { 
      System.out.print("Enter number between 0-6 : ");
      int startFromHere = obj.nextInt();

      System.out.print("Enter number to count position from " + startFromHere + " : ");
      int rotateFromHere = obj.nextInt();
      int absoluteNumber = startFromHere + rotateFromHere;
      System.out.println(absoluteNumber);
      int rotatedNumber = absoluteNumber % 7;
      System.out.println(rotatedNumber);
    }
  }
}

Upvotes: 3

Related Questions