Loops all around! Loops all around!
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