import java.util.Calendar;
import java.io.*;

public class DayOfYear {

public static void main (String[] args) throws IOException {

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
System.out.print("Enter the date (yyyy,mm,dd): ");
String date = in.readLine();
Calendar cal = Calendar.getInstance();
String[] seperate = date.split(",");
int year = Integer.parseInt(seperate[0]);
int month = Integer.parseInt(seperate[1])-1;
int day = Integer.parseInt(seperate[2]);

if(year < 1 || month < 0 || month > 12 || day < 0 || day > 31){
System.out.println("The date is invalid");
}else{

try{
cal.set(year, month, day);

}catch(Exception e){

System.out.println("The format is invalid");

}

int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
System.out.println ("Day of Year :" + dayOfYear);
}
}
}

I keep getting the error Java.lang.indexoutofboundsexception:1

on line 16.

I dont know what I've done wrong with this....can someone please tell me what I've done wrong?

most likely, you provided a date like
"03/04/2012".
so, you create an 'array' using the split method, but you don't split on "/", but on ",".
since there are no ",", your entire String will be in the first element of the array, there will be no second element.
as soon as you try to get the value of the second (non-existing) element, you get that error.

print out the value of separate[0], if it's identical to your input, then that's your problem.

The separator value takes the year value as whenever ur will enter any other format for date except for separator as "," it will show the error and the error is at where u have initialized year...

add the try catch block before the initialization as below and check one

import java.util.Calendar;
import java.io.*;

public class DayOfYear 
{
    public static void main(String[] args) throws IOException 
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(isr);
        System.out.print("Enter the date (yyyy,mm,dd): ");
        String date = in.readLine();
        Calendar cal = Calendar.getInstance();
        String[] seperate = date.split(",");
        System.out.println(seperate[0]);
        try 
        {
            int year = Integer.parseInt(seperate[0]);
            int month = Integer.parseInt(seperate[1]) - 1;
            int day = Integer.parseInt(seperate[2]);
            if (year < 1 || month < 0 || month > 12 || day < 0 || day > 31) 
            {
                System.out.println("The date is invalid");
            } 
            else 
            {
                cal.set(year, month, day);
            }
            int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
            System.out.println("Day of Year :" + dayOfYear);
        } 
        catch (Exception e) 
        {
            System.out.println("The format is invalid");
        }
    }
}

Edited 4 Years Ago by poojavb

This article has been dead for over six months. Start a new discussion instead.