0

This is my first question!

My do-while menu loop does not repeat properly.

It seems to print the default option in the switch every time it repeats after the user has input info into option A or B (Not when X 'exiting'. Rather than going back to the main menu the default switch is printed too along with the another main menu...

It is like it has received an invalid input but no input has been given!

This is my code:

import java.util.Scanner;

public class Part3Final
{

      static double rateM = 1.40;
      static double rateC = 2.40;
      static double rateLCV = 3.80;
      static double rateHCV = 7.20;

      static String M = "M";
      static String C= ("C");
      static String LCV ="LCV";
      static String HCV = ("HCV");

      static String Peak = "Peak";
      static String offPeak = "Off-Peak";
      static String Night = "Night"   ;      

      public static void main(String[] args)
      {

         Scanner sc = new Scanner(System.in);

         System.out.print("\nEnter Vehicle Type (M, C, LCV, HCV):");
         String vehicleType = sc.nextLine();

         if (vehicleType.equals(HCV))
            System.out.print("Enter Trip Time (Peak, Off-Peak or Night):");
            String tripTime = sc.nextLine();     

         String choiceEntry;

         String tripDetails="";
         String tripList= "";

         int sectors;

      // Adjusted rate calculation
         double adjustedHCVNight = (7.2 - (rateHCV*0.3));
         double adjustedHCVPeak = ((rateHCV*0.4)+7.2);
      // Variables to alter later
         double adjustedSectorRate = 100;
         double tripRate = 0;
         double tripCharge= 0;
         double totalCharge=0;

         String breakList = "";
         String breakReport = "";

         double totalBreakCharge = 0;

       //b  char choiceEntryCh = '\0';

         do   
         {
            System.out.println("\nToll Road Data Entry Menu");
            System.out.println("-----------------------------------------");
            System.out.println("\nA - First Option");
            System.out.println("B - Second Option");
            System.out.println("X - Exit");

            System.out.print("\nEnter your selection: ");
            choiceEntry = sc.nextLine();
            choiceEntry = choiceEntry.toUpperCase(); 

            if (choiceEntry.length() != 1)
               System.out.println("Response must be a single character!");
            else

               switch (choiceEntry)
               {
               // equivalent of doing if (selection == 'A' || selection == 'a')
                  case "A":
                  case "a":
                  { 
                     System.out.print("Enter Trip Date:");
                     String tripDate = sc.nextLine();
                     System.out.print("Enter Entry Point:");
                     int entryPoint = sc.nextInt();
                     System.out.print("Enter Exit Point:");
                     int exitPoint = sc.nextInt();

                     sectors= Math.abs(exitPoint-entryPoint);

                     if (tripTime.equals(Peak))
                           tripRate=(adjustedHCVPeak);             
                     if (tripTime.equals(Night))
                           tripRate= (adjustedHCVNight) ;
                     if (tripTime.equals(offPeak))
                           tripRate= (rateHCV);

                     if  (vehicleType.equals(M))
                           tripRate = rateM;
                     if (vehicleType.equals(C))
                           tripRate = rateC;
                     if (vehicleType.equals(LCV))
                           tripRate = rateLCV;

                     tripCharge= (tripRate * sectors);

                     tripDetails = String.format("%s %s %s %d %s %d %s %.2f %s %.2f %s", "- Trip on", tripDate, "from sector", entryPoint, "to sector", exitPoint, "at rate", tripRate, "(toll charge:", tripCharge,")");

                     tripList= (tripList + "\n" + tripDetails);

                     totalCharge= (totalCharge + tripCharge);
                     }
                     break;

                  case "B":
                  case "b":
                  {
                     System.out.print("\nEnter Breakdown Incident Date:");
                     String incidentDate = sc.nextLine();
                     System.out.print("Enter sector breakdown occured in:");
                     int breakdownPoint = sc.nextInt();
                     System.out.print("Enter vehicle recovery cost:");
                     double recoveryCost = sc.nextDouble();

                     breakReport = String.format("%s %s %s %d %s %.2f %s", "- Breakdown on", incidentDate, "in sector", breakdownPoint, "(recovery cost:", recoveryCost,")");

                     breakList= (breakList + "\n" + breakReport);

                     totalBreakCharge= (totalBreakCharge + recoveryCost);

                  }
                     break;

                  case "X":
                  case "x":
                     System.out.println("Exiting data entry menu...");
                     break;

                  default:
                     System.out.println("Error - invalid selection!");
                     break;


                  }     

          }while (!choiceEntry.equalsIgnoreCase("X"));
            //if (choiceEntry.equals("X"))
              //a System.out.println("Exiting data entry menu...");


         System.out.printf("\n%-70s %s\n", "Toll Charges:\n", tripList);

         System.out.printf("\n%s %.2f %s", "(Toll charge total: $",totalCharge,")");

         System.out.printf("\n%-70s %s\n", "\nBreakdown Charges:\n", breakList);

         System.out.printf("\n%s %.2f %s", "(Breakdown charge total: $",totalBreakCharge,")");

         double totalInvoice= (totalCharge + totalBreakCharge);

         System.out.printf("\n%-70s %.2f\n", "\nToll Invoice Total", totalInvoice);


         }


      }

This is print out I get BOLDED is the error:

Enter Vehicle Type (M, C, LCV, HCV):HCV Enter Trip Time (Peak, Off-Peak or Night):Night

Toll Road Data Entry Menu

A - First Option B - Second Option X - Exit

Enter your selection: A Enter Trip Date:6 Enter Entry Point:5 Enter Exit Point:3

Toll Road Data Entry Menu A - First Option B - Second Option X - Exit Enter your selection: Response must be a single character!

NOTE NO RESPONSE HAS BEEN INPUT

Toll Road Data Entry Menu

A - First Option B - Second Option X - Exit

Enter your selection: a Enter Trip Date:5/4/2017 Enter Entry Point:5 Enter Exit Point:2

Toll Road Data Entry Menu A - First Option B - Second Option X - Exit Enter your selection: Response must be a single character!

NOTE NO RESPONSE HAS BEEN INPUT

Toll Road Data Entry Menu

A - First Option B - Second Option X - Exit

Enter your selection: x Exiting data entry menu...

Toll Charges:

  • Trip on 6 from sector 5 to sector 3 at rate 5.04 (toll charge: 10.08 )
  • Trip on 5/4/2017 from sector 5 to sector 2 at rate 5.04 (toll charge: 15.12 )

(Toll charge total: $ 25.20 )

Breakdown Charges:

(Breakdown charge total: $ 0.00 )

Toll Invoice Total 25.20

Can someone please explain why this is happening!

Thank you!

Molecule
  • 3
  • 2

1 Answers1

0

The issue might be in the sc.nextLine() call after the sc.nextInt() call. Since sc.nextInt() doesn't consume the \n newline character and that is probably why you are seeing Response must be a single character! error. See here: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?

MisterMystery
  • 394
  • 5
  • 14