Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Java help

  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    Java help

    Why am I getting an endless loop here?


    public class CarRentalReservationDriver {

    * @param args the command line arguments
    public static void main(String[] args) {

    CarRentalInputOutput io = new CarRentalInputOutput();
    Reservation reservation = null;
    CarRental carRental = new CarRental();
    double choice = io.displayMenu();

    while (choice != 6){
    double carRentalPrice = carRental.calculateCarRentalPrice(choice);

    if (carRentalPrice != 0){

    reservation = io.readCustomerInformation();


    choice = io.displayMenu();

    boolean validPassword = io.readPassword();
    if (validPassword == true)


  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Is choice really supposed to be a double?
    The problem I have with this is that doubles don't really guarantee that they are equivalent to an integer. So instead, use comparisons by either casting to an integer, or using >=/<= type comparisons instead of != when you can. Same goes for the carRentalPrice comparison.

    This assumes the loop condition is being caused by the choice returned, and not caused by a scanner issue like leaving the tokens in place (which happens when you mix strings and numbers without purging the buffers). You are *likely*(?) mixing the two given the displayMenu and readCustomerInformation methods, but I cannot be sure. It is possible the menu is done via scanner, and the readCustomerInformation is done via serialized object file (which it will have to be if you want to stuff that into reservation) and not read from a scanner. That's simply something we cannot tell.

    Walk this all through a debugger instead. Enter per normal and inspect the value of choice when it goes to infinitely loop.

    Also, in the future please select a more suitable title for the thread as well as wrapping your code in [php][/php] or [code][/code] tags.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts