1

Good day to you all, I just want to ask what to do with this.. I want to create program where i can display all the dates inputted without the separator "/" so I used the split method to do it. to be more clear this what I want to do:

Input
Enter Date:10/11/1994
Enter Date:11/10/2008
Enter Date:12/12/2010
Enter Date:08/12/1999
Enter Date:09/10/2005

Output:
10 11 1994
11 10 2008
12 12 2010
08 12 1999
09 10 2005          

The problem is it I have an error in System.out.println(comp[ctr1]); it says that I have to initialize the comp variable, actually I don't what Initialization I will use. I tried using String[] comp=new String[date] and String[] comp=new String[5] but it is still an error.. Thanks in advance..

String[] date=new String[5];
String[] comp;
int mm, dd, yyyy;
for(int ctr=0;ctr<date.length;ctr++){
    System.out.print("Enter Date: ");
    date[ctr]=input.nextLine();
    comp=date[ctr].split("/");
    mm=Integer.parseInt(comp[0]);
    dd=Integer.parseInt(comp[1]);
    yyyy=Integer.parseInt(comp[2]);
}
for(int ctr1=0;ctr1<date.length;ctr1++){
    System.out.println(comp[ctr1]);
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
NEETRX-78
  • 23
  • 2
  • 3
    And what was the error with `String[] comp=new String[5]`? – Tom Apr 18 '16 at 11:32
  • 1
    Voting to close as typo / cannot reproduce since `String[] comp=new String[5]` works and OP tried it (?). – Tunaki Apr 18 '16 at 11:36
  • it says ArrayIndexOutOfBoundsException during runtime – NEETRX-78 Apr 18 '16 at 11:37
  • @Tunaki guess he´s talking about the follow up `ArrayIndexOutOfBoundException`, which will occur because `comp` is used to store each date, aswell as each splittet part of the dae. – SomeJavaGuy Apr 18 '16 at 11:37
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – SomeJavaGuy Apr 18 '16 at 11:39
  • 1
    Your problem is with this line `comp=date[ctr].split("/");`. Here you replace your comp array for a new one with length 3 (containing the dd, mm, and year). – Kevin Cruijssen Apr 18 '16 at 11:40

3 Answers3

1

Why reinventing the wheel? Homework? Why don't use java tools to achieve your goal?

DateFormat input = new SimpleDateFormat("MM/dd/yyyy"); 
DateFormat output = new SimpleDateFormat("MM dd yyyy");

Date d = input.parse("10/11/1994");
System.out.println(output.format(d));

OUTPUT:

10 11 1994
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

you have first to split using the "dd/mm/yyyy"
String [2] dateFormate= date[i].split[":"] ;
the make your processing on the dateFormate[1] ; i think every thing will be fine

mhassan
  • 11
  • 3
0

First of, String[] comp=new String[5] does work, it will remove the compiler error, the follow up error occurs, because you are accessing Indexes of an array which are out of the length of the array. Technically this shouldn´t occur, because the declaration String[] comp=new String[5] will make your array have the same size as the date array, which you use as the second loop condition. But your mistake happens inside the first loop.

You reinitialize the comp array, which if the input is correct, will have the size 3 now, instead of the size 5. And since your second loop will use the date array as condition, which does still have the size 5, you´ll try to acces the element 3 and 4 in the comp array, which are out of the length of the freshly reassigned array. That´s the code causing the problem comp=date[ctr].split("/");.

Using your method, i´d suggest turning the comp array in a 2d array, in order to store the amount of dates, and the parts of the date.

The code might be looking like this now:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String[] date=new String[5];
    // make comp a 2d Array
    String[][] comp = new String[5][];
    int mm, dd, yyyy;
    for(int ctr=0;ctr<date.length;ctr++){
        System.out.print("Enter Date: ");
        date[ctr]=input.nextLine();
        // At index ctr store the different parts of the date
        comp[ctr]=date[ctr].split("/");
        // This three steps can cause an ArrayOutOfRangeException, 
        // because the indexes 0,1,2 are depending on how many elements the split returned
        // You might want to add this here, but i´ll leave it out.
        // But since you never use mm, dd and yyyy you may aswell leave it out
        mm=Integer.parseInt(comp[ctr][0]);
        dd=Integer.parseInt(comp[ctr][1]);
        yyyy=Integer.parseInt(comp[ctr][2]);
    }
    for(int ctr1=0;ctr1<date.length;ctr1++){
        for(int j = 0;j<comp[ctr1].length;++j) {
            System.out.println(comp[ctr1][j] + " ");
        }
    }
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • Thanks sir for helping it is now running as I want it to be.. by the way what if I want to sort this dates ascending or descending?? Is it possible to sort it using the bubblesort method?? I only learned this last week so I am not fully sure how to apply it on inputs like a date/time inputs.. – NEETRX-78 Apr 18 '16 at 11:55
  • @NEETRX-78 you just loop over the `comp` array and you can simply compare the values at index `0-2`. – SomeJavaGuy Apr 18 '16 at 13:04