Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:10:21+00:00 2026-06-01T09:10:21+00:00

I’m pretty new to Java and tried the Best Before puzzle from Spotify yesterday.

  • 0

I’m pretty new to Java and tried the Best Before puzzle from Spotify yesterday. I get the “Wrong Answer” error when I send it in.
Checking for other solutions didn’t help and I cant figure out which input gives a wrong answer.
At best you would just tell me which code lead to the wrong answer. I should be able to correct the code myself then.

import java.util.Scanner;

public class Bestbefore {

public static void main(String[] args) {
     //Process Input String
    Scanner scanner = new Scanner(System.in);
    String dateString = scanner.nextLine();
    Integer a,b,c;
    a=Integer.parseInt(dateString.substring(0,dateString.indexOf("/")));
    b=Integer.parseInt(dateString.substring(dateString.indexOf("/")+1,
                        dateString.lastIndexOf("/")));
    c=Integer.parseInt(dateString.substring(dateString.lastIndexOf("/")+1));
    int[][] va={            //All possible combinations
            {a,b,c},
            {a,c,b},
            {b,a,c},
            {b,c,a},
            {c,a,b},
            {c,b,a}
    };
    int i=0;
    while (!checkDate(va[i][0], va[i][1], va[i][2]) 
            && i<5) // get the first valid date combination
        i++;
    //to prevent OutofBoundsException of the while loop
    if (!checkDate(va[i][0], va[i][1], va[i][2]))           
            i++;

    if (i==6)            
        System.out.println(dateString+" is illegal");
    else
    {   //compare the rest of the va-Array and save the position of the lowest Date in i
        for (int k=i+1;k<6;k++)                                                     
            if (checkDate(va[k][0], va[k][1], va[k][2]))
            {
                if (ageOfDate(va[k][0], va[k][1], va[k][2]) 
                        < ageOfDate(va[i][0], va[i][1], va[i][2]))
                    i=k;
            }
            System.out.println(getDate(va[i][0], va[i][1], va[i][2]));
    }
}
public static boolean checkDate(int day,int month, int year)
{
    int[] dpm={31,28,31,30,31,31,30,31,30,31,30,31};    //Days per Month
    if (year<2000)
        year=year+2000;
    if((year%4==0 && year%100!=0) || year%400==0)
        dpm[1]=29;                                      //Leapyear correction
    if (month==0 || month>12)
        return false;
    else
        if (day==0 || day>dpm[month-1])
            return false;
    else
        return true;
}
public static int ageOfDate(int day,int month, int year)    //to compare 2 dates
{
    return ((year*10000)+(month*100)+day);
}
public static String getDate(int day,int month, int year) //for the Output
{
    String smonth = String.valueOf(month);
    String sday = String.valueOf(day);
    if (year<2000)
        year=year+2000;
    if (month<10)
        smonth="0"+smonth;
    if (day<10)
        sday="0"+sday;
    return (year+"-"+smonth+"-"+sday);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:10:22+00:00Added an answer on June 1, 2026 at 9:10 am

    June has 30 days, August and July both have 31. Your array dpm is initialized in a wrong manner. So here is where you can go wrong: 31/06/20 -> earliest possible is 20-th of June 2031 instead of 31-st of June 2020.

    Hope this solves your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.