TIL: Types Are Hard
More java today, still working through sample problems. I had expected to make it through a few today seeing as I’d already done them on the EcmaScript course but…. no such luck. Things are just too different. After about 40 minutes of googling I finally made it through a pretty straight forward problem.
One thing is pretty obvious at this point: I need to do some kind of formal class/learning. I was really hoping to just kinda learn as I was moving along through these lessons but there’s a whole lot of other stuff that I’m missing out on doing things this way. Yeah sure I’m learning some of the syntax, but I still don’t even know how to compile or run a program. Everything’s just been running Unit Tests (which is cool), but I feel like there’s a lot I’m still in the dark on.
Anyways, the problem I worked through today got me some hands on work with
char arrays and iterating through them. Which also meant dealing with type casting which I vaguely remember from 10 years ago!
Let’s just look at the solution for the problem I worked:
There’s a lot going on here. For my sake of remembering, I’ll step through each piece.
First is the
char array. We’re setting this equal to an array of all of the individual digits in
numberToCheck. First we convert the int to a String, and then convert the string to an array of characters. Whew.
After we’ve got the array, we then iterate through each of the digits. In this case I chose to go with Java’s version of a foreach loop, instead of a normal for loop. With this you only get the value of each item in the array, no index values. Just like the
for..of in JS.
When we iterate through, we have to convert each of the characters back to a number before we can do the exponent math on them. To do so, I used a Character method to grab the numeric value (since they’re digits in this case) and performed the
Math.pow function. This returned back a double result which needed to be turned back into an int so that it could be added to the final value.
Last we do our check to see if the value calculated matches the value that was received and return back the result.
Super weird dealing with types again after so long. I’m sure I’ll grow to appreciate them but they’re still so foreign at this point.