Results 1 to 3 of 3
  1. #1
    daemonlies is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Return to an input that was wrong!!

    Hi everyone,

    I'm trying to write a program that calculates the parking time and cost. I have an error mesage stating that the 'Exit time must be less than Entry time' if enter incorrectly. What my issue is that, i have to redo the inputs again, where i only want to re-enter the exit time. What process do i need to follow to be able to return to the exit time and continue with the rest of the program.

    public class lot {

    public void charge()
    {
    Scanner input = new Scanner( System.in );

    customer = 1;

    while ( customer <= 2 )
    {

    System.out.printf("\nPlease enter your Entry & Exit times "
    + "(e.g. 0953) for Customer %d\n", customer );

    System.out.printf( "Enter Entry Time: " );
    entryTime = input.nextInt(); // read entry time


    System.out.printf( "Enter Exit Time: " );//
    exitTime = input.nextInt(); // read exit time

    calculateCharges();


    System.out.printf( "Your parking charge is: $%d.00\n", charges);


    customer = customer + 1;

    receipt = receipt + charges;
    }

    System.out.printf("\n***************************** **************"
    + "********\n\nRecipt: Total charges for all customers"
    + " are: $%d.00\n\n**************************************"
    + "*************\n\n", receipt);
    }

    public void calChar()
    {

    entry = (entryTime / 100)* 60 + (entryTime % 100);
    exit = (exitTime / 100)* 60 + (exitTime % 100);
    minutes = exit - entry;


    if (exitTime < entryTime)
    System.out.println("\n** Error: Entry time must be less than "
    + "Exit time **\n");


    else if ( minutes <= 15 )
    charges = 0;


    else if ( minutes <= 30 )
    charges = 2;


    else if ( minutes <= 60 )
    charges = 4;

    else if ( minutes <= 120 )
    charges = 6;


    else if ( minutes <= 180 )
    charges = 8;


    else if ( minutes <= 240 )
    charges = 10;


    else if ( minutes <= 300 )
    charges = 12;


    else if ( minutes <= 360 )
    charges = 14;


    else if ( minutes <= 420 )
    charges = 16;


    else if ( minutes <= 480 )
    charges = 18;

    else
    charges = 20;
    }
    }

    Any help would be much appreciated

    Daemonies

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: Return to an input that was wrong!!

    Instead of your if statement you can use a while loop with the same condition. Inside the while loop you would ask the user for the exit time again and redo your calculation. Once the condition is met it will continue with the program.

    Something like:
    Java Code:
    while (exitTime < entryTime){
           System.out.println("\n** Error: Entry time must be less than "
                  + "Exit time **\n");
          System.out.printf( "Enter Exit Time: " );//
          exitTime = input.nextInt(); // read exit time
    }
    EDIT: I just reread your code and I think you would be better off moving this while loop above your calculations of exit, enter, and minutes so you don't have to recalculate them when the exitTime changes
    Last edited by yellowledbet; 03-30-2012 at 05:48 AM.

  3. #3
    daemonlies is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Return to an input that was wrong!!

    Thank you very much .. worked out fine

    Daemon

Similar Threads

  1. Stopwatch return wrong values
    By deadManN in forum New To Java
    Replies: 9
    Last Post: 12-22-2011, 02:38 PM
  2. if wrong input, you get a new chance
    By hreodbeohrt in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 03:07 AM
  3. method return statement is wrong
    By ftrengnr in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 05:15 AM
  4. Handling Errors IE: Wrong User Input
    By movsesinator in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 02:37 AM
  5. handle wrong input int/null
    By Camden in forum New To Java
    Replies: 1
    Last Post: 12-16-2007, 10:37 PM

Posting Permissions

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