Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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){
    io.displayCarRentalPrice(carRentalPrice);

    reservation = io.readCustomerInformation();
    reservation.setCarRentalPrice(carRentalPrice);


    io.displayConfirmation(reservation);
    }

    choice = io.displayMenu();
    }

    boolean validPassword = io.readPassword();
    if (validPassword == true)
    {
    io.displaySummary(carRental);
    }

    System.exit(0);
    }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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'); 


  •  

    Posting Permissions

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