Skip to content
Coding Fanatic
  • Android Development
  • News and Reviews
  • Road2Pro
  • Tutorials
  • Services
  • About

Return of the Java – 95 of 100 Days…

  • March 11, 2021March 12, 2021
  • by Richard Clarke

Just when I thought I was out, Java keeps pulling me BACK!

For more on this, watch the video a the bottom of the page!

I restarted a the KeepHydrated codewars challenge. Last time, I took a more long-winded approach and ended up lost. This time around I made it much smoother.

PSEUDOCODE


My new pseudocode for this challenge goes as follows:

  1. Import the Scanner class from Java
  2. Create Scanner object for obtaining input
  3. Prompt for time as a Double
  4. Multiply by .5
  5. Truncate the decimal using the toInt() method 
  6. Display the liters of water consumed.

IMPORT THE SCANNER CLASS FOR OBTAINING USER INPUT


Last time, I used the readLine() method to obtain input from the keyboard. However, this is better for obtaining Strings than numbers.

Yes, the toInt() method allowed me to truncate the decimal from the user’s input. But while testing input using decimals, I found it could throw exceptions and crash the program.

By using the Scanner class, I could parse input for specific data types like doubles, float, etc. In this case, I could parse input for a double, store it, and do the math later.

MATH AND ROUNDING DOWN


Once I obtained the number of hours, all I had to do was multiply by .5 to get the amount of water. Finally, the toInt() method would truncate the decimal leaving us with the final answer rounded down!

I thought I could put my Java days behind me. Little did I know it still had some lessons for me. Kotlin doesn’t have a way to obtain decimals in terminal based programs.

The only option is to import the Scanner class from Java. From there, you can set up your program to get the type of input you want.

It took a lot of digging and experimenting to find this. I ran into a lot of errors, but I got there in the end.

Sure, I likely won’t use Kotlin for terminal based programs in my career. But this is an idea worth exploring. I gained some insight on different ways to use Kotlin and saw some connections with Java I didn’t know about.

Oh, and I’m definitely ready to complete the challenge on codewars.

Join the mailing list to see updates like this every week!
Coding Fanatic

Related

Five Tips for Socializing Online – Road 2 Pro 10
Five Things to Avoid When Socializing Online – Road 2 Pro 11
Richard Clarke
Richard is currently a developer using Java and Android Studio to build custom applications for Android. Richard tutored in mathematics at Montgomery College, and worked in Information Technology before entering the Software Development industry as a Quality Engineer.
100daysofcode Android Android Development AndroidDev development Java kotlin software software development

Related articles

Your New Favorite Stopwatch App!…
Two for Two BABY! 7…
…But the App is for…
Play Store Blues – 5…
Top 4 Tips for Android…
No Dice – 3 of…

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • GitHub
  • YouTube
  • X
  • Instagram
  • LinkedIn
My Tweets

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • X
  • GitHub
  • LinkedIn
  • YouTube

Archives

Theme by Colorlib Powered by WordPress