+ 2

Please help with kotlin!

I'm reading a .txt file and saving it in a list The list looks something like this: 2012 1 10.30 2012 2 6.00 2012 3 17.45 . . . 2019 1 11.45 Basically, it's a list of months and spending for each month. Now I want to calculate the year with the highest spending and the lowest spending. How would I go about doing this, I have tried to do a for loop that compares year[i]==year[i+1] and if true adds spend[i] to total spends but this doesn't work as I get an indexOutOfBounds exception. Thank you for any help in advance!

7th Mar 2020, 12:40 PM
MBanski
MBanski - avatar
14 Answers
+ 6
acc is the accumulator which is the spending in ur case. e is the element. in this case one line if your file. Additionally we have a rule of how to accumulate.
7th Mar 2020, 5:17 PM
Oma Falk
Oma Falk - avatar
+ 5
MichaelS I am still learner too. It was a pleasant challenge for me.
7th Mar 2020, 5:33 PM
Oma Falk
Oma Falk - avatar
7th Mar 2020, 3:24 PM
Oma Falk
Oma Falk - avatar
+ 3
https://code.sololearn.com/cNNhxjGtq934/?ref=app
7th Mar 2020, 4:04 PM
Oma Falk
Oma Falk - avatar
+ 2
Oma Falk I appreciate all the help, Thank you!
7th Mar 2020, 5:27 PM
MBanski
MBanski - avatar
0
Oma Falk would this work if my list isn't hard coded in? I'm making my list by reading a file and using a loop. So can I do var spend = mutableListOf() and then do spend.add(year, month, spend) to make the list?
7th Mar 2020, 3:45 PM
MBanski
MBanski - avatar
0
MichaelS yes but maybe spend.add(listOf (year, month, spend))
7th Mar 2020, 3:48 PM
Oma Falk
Oma Falk - avatar
0
Oma Falk unfortunately this gives and error
7th Mar 2020, 3:55 PM
MBanski
MBanski - avatar
0
can u show ur orig please?
7th Mar 2020, 3:56 PM
Oma Falk
Oma Falk - avatar
0
wait...since we add, spend must be a mutable List!
7th Mar 2020, 3:56 PM
Oma Falk
Oma Falk - avatar
0
Oma Falk I'm not sure if that's what I was meant to do. However, I don't understand the error
7th Mar 2020, 3:59 PM
MBanski
MBanski - avatar
0
Kotlin could not infer the type of list elements because spend was an empty list. In this case we have to specify it like I did.
7th Mar 2020, 4:09 PM
Oma Falk
Oma Falk - avatar
0
Oma Falk just for learning purposes what is the "acc" and "e" in the acc, e->acc+e[2] the represent?
7th Mar 2020, 4:40 PM
MBanski
MBanski - avatar