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

Loops all around! Loops all around!

  • February 21, 2017January 10, 2018
  • by Richard Clarke

This week I’m working on an application for converting between different lengths and weights.

Users will select an option to convert a distance or weight, then a menu will be displayed where they can select different units of measurement to convert between.

I’m using loops since they can automate specific parts of my program. There are a few blocks
of code that I want to run a certain number of times and, using loops, I’m right in the driver’s seat.

I wanted the program to give users the option to perform additional conversions after the first one. In other
words, this program needs to run at least once. Using a do-while loop, I can have the program run once and then, depending on the user’s response, the program can either terminate or run again! The result is a more interactive program controlled by the user.

I ran into some issues when prompting the user to enter ‘Y’ to continue or ‘N’ to quit. It seems the Scanner
method nextLine() was adding a return, or rather, simulating someone pressing enter before I could even get input from the keyboard. This resulted in the program asking the user to enter Y/N, then immediately asking again before receiving any input.

After replacing these instances with the next() method from the Scanner class, the program only displayed the prompt once before accepting input and continuing to run. Now I know to use the next() method when obtaining shorter Strings as input.

Moving forward, I believe designing a menu for each different type of conversion will be the most challenging. The program has to be able to convert between any measurement of distance or amount of weight. I’ll have to come up with a way to manage the different conversions so that, depending on a user’s wants, the program can deliver accurate results.

I really want to continue this project as I delve back into Java. i enjoy projects that can be customized and
constantly added to. You will definitely hear more about this in future posts.

-CF

Related

My story is complete…ish
Kick That Boulder to Your Heart’s Content
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.
Class Java Loops Scanner

Related articles

Kick That Boulder to Your…
My story is complete…ish
Interactive Fiction: (F)Funny (U)Unreal (N)No…
Interactive Fiction: A Fun Program…
Falling Through and Landing on…
Condensing Your Code with Variables,…

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