0

Below code is giving result two times but it should be one result.

import java.io.*; import java.util.*; public class DateAfterArrayTest { public boolean[] dateAfterArrayTest(Date[] dtArrVl1) { int cnt = 1; for (int i=0; i<dtArrVl1.length; i++) { System.out.println("Date value " + cnt + " is :" + dtArrVl1[i]); cnt ++; } System.out.println(); boolean[] rslt = new boolean[dtArrVl1.length]; for (int j=0; j<rslt.length; j++) { if (j != rslt.length-1) { rslt[j] = dtArrVl1[j].before(dtArrVl1[j+1]); } } return rslt; } public static void main(String[] args) { Date[] dtArr1 = {new Date(2017, 01, 15), new Date(2018, 01, 15)}; DateAfterArrayTest dateAfterArrInstnc = new DateAfterArrayTest(); boolean[] rsltVl = dateAfterArrInstnc.dateAfterArrayTest(dtArr1); System.out.println("Comparing date 1 value with date 2 :"); for(boolean fnlRslt : rsltVl) { System.out.println("Date after value is : " + fnlRslt); } } } ------------------------------- Output: Date value 1 is :Thu Feb 15 00:00:00 UTC 3917 Date value 2 is :Fri Feb 15 00:00:00 UTC 3918 Comparing date 1 value with date 2 : Date after value is : true Date after value is : false The last line of output should not come because in for loop if condition will fail at second interation. But in the retuning boolean value i'm having two results. Can anyone clarify this issue.

24th Oct 2017, 2:24 AM
Naagabaabu
8 ответов
+ 4
Are you referring to this line of output as the one that shouldn't be output? Date after value is : false If so, this is because you're using a for each style for loop. This will go over each value within the array. The type of itterable doesn't matter. You don't have an if statement that checks if it is false within the loop. try: for(boolean fnlRslt: rsltVl) { if(fnlRslt) System.out.println("Date after value is : " + fnlRslt); }
24th Oct 2017, 2:43 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
If you truly only want one output in the result array, change your allocation and setting code to: boolean[] rslt = new boolean[dtArrVl1.length-1]; for (int j=0; j<rslt.length; j++) { rslt[j] = dtArrVl1[j].before(dtArrVl1[j+1]); } It will only allocate the array elements being set in your code. Your existing code allocated 2 elements, but only set the first, which is why you got 2 outputs. This new code would work with 3 dates in the date array yielding 2 elements in the result array.
24th Oct 2017, 5:46 AM
John Wells
John Wells - avatar
+ 4
The Date(year, month, day) constructor is Deprecated so it shouldn't be used. The year is expected to be desiredYear - 1900, which is why you got 3917/3918. The month is expected to be 0 to 11 so you got February with 1, if you expected January pass 0.
24th Oct 2017, 5:07 AM
John Wells
John Wells - avatar
+ 2
Apart from what @ChaoticDawg explained, why the loops? I'm sure you know how to compare 2 Dates without loops, it could've been done with an if statement, and your method returns comparison result. I can understand it if you say you're comparing multiple Dates, though I don't see a reason for people comparing multiple Dates. I also noticed that the Dates were interpreted wrong, your Date inputs' year were 2017 and 2018, but the output displayed 3917 and 3918, there's a 1900 year gap in between, idk if it was intentional, but otherwise you might need to look at it. (Edit) @ChaoticDawg, I guess he was referring to the "for int j..." for loop in the method, there's an if in it, but it only prevent evaluation of the second Date, because there's nothing to compare to after. It wouldn't stop the "for(boolean fnlRslt..." loop anyway. You were right mate. Hth, cmiiw
24th Oct 2017, 3:48 AM
Ipang
+ 2
Hi @Naagabaabu, Well if it's for practicing then great, I was just making notes on some points, now I understand the reason :) Keep moving mate!
24th Oct 2017, 4:12 AM
Ipang
+ 1
Hi chaotic Thanks for clarification.
24th Oct 2017, 3:54 AM
Naagabaabu
+ 1
Hi Ipang Here I'm using multiple dates because I want to return multiple values from class method. In traditional and in common most of the time we are returning single value in return. But as a matter of practice I'm returning multiple values in return statement. so each and every time I'm practicing every program by returning single value and next program I'm returning multiple values by using arrays. Thanks for clarification.
24th Oct 2017, 4:00 AM
Naagabaabu
+ 1
Hi John I modified the code for year value to 117 and 118 and month value to 0 to get january. Thanks for clarification. With below code i'm getting the desired output from method level. boolean[] rslt = new boolean[dtArrVl1.length-1]; for (int j=0; j<rslt.length; j++) { rslt[j] = dtArrVl1[j].before(dtArrVl1[j+1]); }
24th Oct 2017, 4:44 PM
Naagabaabu