The compareTriplets function will compare between array list of a and b. Easch array list has 3 elements.
if a>b then score for a is increased by 1, else if a==b do nothing if a<b then score for b is increased by 1
Here, I want to return an array list of compareTriplets function which will have 2 integer values of a and b. Then use this array list in the main function.
I'm not able to return an ArrayList from the compareTriplets function. can you please give me a hint on that.
Thank you very much for your explanation and help. How can I return two values as an arraylist where I have counter1 and counter2 values?
I want to store it in arraylist and want to return that arraylist from Comparetriplets method.
I would suggest you are better off creating an IntegerTriplet class or similar, with comparison methods. Make that method return a List<Integer>; it is probably a good idea to make that List immutable or read‑only.
Local variables can't have same name. In method compareTriplets you have counter1, counter2 named variables of type int as well as List<Integer> which gives compile time error.
Always post complete compiler error message ( In text only, no images Or videos ) if code doesn't compile. If runs successfully and not expected output then post the current output as well as the output you are expecting.
You don't need to put hard coded index value to access element of List<Integer> a and List<Integer> b like you did in your code.
You have for loop ranging index values of j from 0-2 so use j variable as an Index like
You can use method of Arrays class i.e. Arrays.asList() to return an array as List in method compareTriplets.
You don't need to create another two variables of List<Integer> type as in your case you tried to create List<Integer> counter1 and counter2.
I know it is a bit late, but the old Sun Style Guide suggest you avoid constructs like if (b) return x; else return y; It recommends you use the ?: operator instead. Note that the else keyword is redundant in that sort of construct.
The ?: operator can be nested inside itself, but you need to be very careful to indent the code and maintain legibility.
Now the error is run time error. Exception in thread "main" java.lang.NullPointerException at Solution.main(Solution.java:82).
And I'm not sure about the return of arraylist. Is that correct? Can you please help me regarding this?
Your main method is much much too long. Don't put all the logic in it; the ideal length of a main method is one statement.
Don't use multiple write instructions; that will risk slow execution. Use a StringBuilder or a StringJoiner to collect the text and write it all at once. A StringJoiner will obviate the need to test whether you have reached the end of the series, and you can simply truncate a StringBuilder by 1 character.
Don't close your reader and writer explicitly; use try with resources instead.
I also think you need separate reading and writing methods.
I wouldn't use a reader and Integer#parseInt; I would use a Scanner.
There is something wrong about your using a regular expression to trim the String when you could use String#trim instead. In fact if you split on multiple whitespace ("\\s+"), you may not need to trim the String at all.
There seems to be a serious logic error in your method creating the List. Apart from the fact that the method doesn't look at all object‑oriented, you have a situation whereby you can return null; I shall leave you to work out which circumstances will create a null there. Line 44.
Why are you returning null when elements of a and b are same, here on line 43-45
Suppose you have elements of a and b as follow
in above scenario your counter1 should be 2 and counter2 should be 0 but in your code when elements of a and b are same instead of doing nothing you are returning null value to the place where this compareTriplets method was invoked from i.e on line 80
which assigns null value to result variable.