4

I'm having an issue with some of my code. I'm receiving a date in the dd/mm/yyyy format as a string called dateofQ.

I want the date to be yyyy_mm_dd, I'm using a string.split() into an array, but it wont return the 3rd array called myArr[3]:

String[] myArr = dateofQ.split("\\/");
String dateFormat = String.format("%s_%s_%s",myArr[2],myArr[1],myArr[0]);

It returns myArr[1] and myArr[0], but when I also add myArr[3] I get an issue at runtime:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at ReadFile.main(ReadFile.java:34)
3
  • Sample input plz! I mean what did you give in dateofQ Commented Oct 4, 2012 at 7:21
  • 2
    I have tried your code and it works for me. Are you sure that the date really has the dd/mm/yyy format? Commented Oct 4, 2012 at 7:29
  • Uh. Guys. Again, the OP edited this to read myArray[2] instead of myArr[3], but someone else was also editing it and it overwrote his edit. :-\. Commented Oct 4, 2012 at 7:46

4 Answers 4

6

Well, the array only has 3 elements, and myArr[3] is trying to get the fourth element (remember, arrays are zero-indexed).

To get the third element, use myArr[2].

Sign up to request clarification or add additional context in comments.

4 Comments

He edited it to refer to myArr[2] right as you posted this.
Actually, someone else edited it while he was editing it and overwrote his change, so I put it back in.
Array index happens at 2 index so this is not a solution to his problem.
Well, the OP does say myArr[3]. The OP edited this to say myArr[2], but someone was also editing his post at the same time and overwrote his changes. I edited that back in, but it hasn't been accepted as an edit yet.
2

It looks like the length of myArr is at most 2.

Please check to ensure myArr.length is not at least 3.

Inserting

System.out.println(myArr.length);

right after:

String[] myArr = dateofQ.split("\\/");

is an easy way to check this.

1 Comment

He should check the length Array Index happened at 2 so there are not enough elements in the array.
2

I'm probably missing something really obvious, but couldn't you just do something like...

try {
    String dateofQ = "08/03/1972";
    SimpleDateFormat in = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat out = new SimpleDateFormat("yyyy_MM_dd");

    dateofQ = out.format(in.parse(dateofQ));
    System.out.println(dateofQ);
} catch (ParseException ex) {
    ex.printStackTrace();
}

Which out puts 1972_03_08

1 Comment

Thanks this did it. I've never heard of SimpleDateFormat before (I am a noob). I did need to add import java.text.SimpleDataFormat and import java.text.ParseException to get it to work. Thanks again
0

ArrayIndexOutOfBoundsException: 2 This error means you have array of size less than 3 elements and you are trying to access third. i.e. you will get error @ myArr[2].

What you should do is you should check length of array first and then access it. If invalid array reject it.

if(myArr.length==3)//Process else you need to reject

A better way could be use way specified in MadPorgrammer's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.