• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

While loop help

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, everyone. I am writing a program that I need to use the while loop for. I need to ask for the speed and number of hours traveled. It has to use a loop to display the distance the vehicle has traveled for each hour.
I need to show a chart after, like:
Hour Distance Traveled
-----------------------------
1 40
2 80
3 120

I have gotten some of it so far, I just have no clue how to get the values appear with the chart. Thanks a ton!


 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by moving all that code out of the main method. A main method should contain 1 statement, ideally. Everything else should be written elsewhere.
I think it is possible to confuse yourself by trying too much. And I think you have managed that. I think you need to start small. Do one thing and one only. Try running at 40mph for 1hour and see whether you can get it to print 40miles. Then try going for 2 hours and getting it to print 40miles … 80miles. That will of course be in a loop which runs twice.

Don't use doubles if you can possibly avoid them. Use ints. That way you won't have to bother about whether you have driven for 2½hours and why it won't show 100miles. Also you can get horrible errors if you use floating‑point arithmetic to control loops. Example: Guess how often the following loop will run:-I shall give you a hint. If you look in the BigDecimal(double) constructor documentation, you find that the nearest double to 0.1 is very slightly higher than 0.1.
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware. I cannot tell the difference between a hint and a red herring. Mwaahaahaahaahaahaahaahaahaahaa!
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From your posted code, it looks like you want to print the table after the input is over. If that is true, you must collect the data.

You are using data pairs: speed and time. In doing so you have many options to follow as to how to declare your data collection base object, but you have a flagged exit in your collection, so you do not know how many times through the loops you will go, so you don't know how many of the data pairs you will need.

You can declare a data type that consists of 2 of your chosen data types, but I prefer to keep things as simple as possible, so I would think of an array--let's say you are using int as your base type. so

will give you a data pair of int--m[0] and m[1]. Since you do not know how many iterations through the loop you will do, you need to be able to save these off in a collection; I prefer to use an ArrayList to do so.

so after you collect your data in the loop in your array m and determine that you need to save it, then put it in your ArrayList before you loop again.


to print it out you will need to do something along this line, but format it as you need to do so:
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect the intention behind the exercise is to loop for 1hr 2hrs 3hrs etc and each time print the time and distance. That can be don simply with one loop.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you "need to use a while loop"? Generally, if you know exactly how many times you are going to loop, a for loop is preferred. Granted you can pretty much always use any of looping constructs...I just find it odd when anyone says "I have to do THIS using THIS".
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the exercise said, “Use a while loop.”
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic