asdfg
asdfg

Reputation: 3

Finding the day ahead after n days

I'm writing simple code in Java which is supposed to return the day after n days. But because I just started learning I'm having problem solving that. For now, I wrote that piece of code but I have no idea how to move forward.

import java.util.Scanner;
public class Program {
    public static void main(String[] args) {
        
        final String Mon = "Mon";
        final String Tue = "Tue";
        final String Wed = "Wed";
        final String Thu = "Thu";
        final String Fri = "Fri";
        final String Sat = "Sat";
        final String Sun = "Sun";
        
        String[] arr = {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
        
        System.out.println("What day is today:");
        Scanner scanner = new Scanner(System.in);
        String text = scanner.next();
        
        System.out.println("You want to know the day after how many days:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        if(n>=7)
            n = n%7;
        
        int[]index = {0, 1, 2, 3, 4 ,5 ,6};
        
        String start;
        
        switch(text) {
        case Mon:
            start = arr[0];
            //int ind = index[0];
        case Tue:
            start = arr[1];
            //int ind = index[1];
        case Wed:
            start = arr[2];
        case Thu:
            start = arr[3];
        case Fri:
            start = arr[4];
        case Sat:
            start = arr[5];
        case Sun:
            start = arr[6];
        
        for(int i =0;i<n;i++) {
            String solution = arr[index[i]+i];
        }
        System.out.println(solution);
        }
    }

Upvotes: 0

Views: 220

Answers (3)

k314159
k314159

Reputation: 11062

You have a lot of unnecessary statements. All you need to do is take the index of the given day, add the number of days, modulo 7, and get the resulting element. Java 11 solution:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

        List<String> daysList = List.of("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");

        Scanner scanner = new Scanner(System.in);
        System.out.println("What day is today:");
        String today = scanner.nextLine();

        System.out.println("You want to know the day after how many days:");
        int numberOfDays = scanner.nextInt();

        System.out.println("The day after " + numberOfDays + " will be "
                + daysList.get(((daysList.indexOf(today) + numberOfDays) % 7)));
     }
 }

Upvotes: 0

ubaid shaikh
ubaid shaikh

Reputation: 453

I renamed arr to daysList and n to numberOfDays. Also,

  • You do not need the index array

  • You can get the index of today's day by iterating over the daysList array using a for loop as follows

  • After getting the current index, we just need to add numberOfDays to the current index and get the index of the required day. (Please note, we have to do %7 here to bring back the index under 7).

  • The updated code is below for your reference

     import java.util.Scanner;
     public class Main {
         public static void main(String[] args) {
    
             final String Mon = "Mon";
             final String Tue = "Tue";
             final String Wed = "Wed";
             final String Thu = "Thu";
             final String Fri = "Fri";
             final String Sat = "Sat";
             final String Sun = "Sun";
    
             String[] daysList = {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
    
             System.out.println("What day is today:");
             Scanner scanner = new Scanner(System.in);
             String today = scanner.next();
    
             System.out.println("You want to know the day after how many days:");
             Scanner sc = new Scanner(System.in);
             int numberOfDays = sc.nextInt();
    
             if(numberOfDays >= 7)
                 numberOfDays = numberOfDays % 7;
    
             int ansDayIndex = 0;
             for(int dayIndex = 0; dayIndex < 7; ++dayIndex){
                 if(daysList[dayIndex].equalsIgnoreCase(today)){
                     ansDayIndex = (dayIndex + numberOfDays) % 7;
                     break;
                 }
             }
    
             System.out.println("The day after " + numberOfDays + " will be " + daysList[ansDayIndex]);
         }
     };
    

Upvotes: 2

pxm
pxm

Reputation: 1611

  • If you want to use switch-case , try using break in between

  • Instead of forloop you can simply do like this

  import java.util.Scanner;
  public class Program {
  public static void main(String[] args) {

            
            final String Mon = "Mon";
            final String Tue = "Tue";
            final String Wed = "Wed";
            final String Thu = "Thu";
            final String Fri = "Fri";
            final String Sat = "Sat";
            final String Sun = "Sun";
            
            String[] arr = {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
            
            System.out.println("What day is today:");
            Scanner scanner = new Scanner(System.in);
            String text = scanner.next();
            
            System.out.println("You want to know the day after how many days:");
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            
            if(n>=7)
                n = n%7;
            
            int[]index = {0, 1, 2, 3, 4 ,5 ,6};
            
            int start=-99;
            
            switch(text) {
            case Mon:
                start = 0;break;
            case Tue:
                start = 1;break;
            case Wed:
                start = 2;break;
            case Thu:
                start = 3;break;
            case Fri:
                start = 4;break;
            case Sat:
                start = 5;break;
            case Sun:
                start = 6;
            }
            
            String solution = arr[(start+n)>=7?(start+n-7):(start+n)];
        
            System.out.println(solution);
            }
        }

Upvotes: 0

Related Questions