1

I cannot get the variable adjustedCharge to print out (the text is printed but not the value) I have tried to trace but still cannot get it to print properly. No errors are present either.

import java.util.Scanner;
public class Assignment {

    public static void main(String[] args) {
        //Declare scanner
        Scanner input = new Scanner(System.in);
        //Prompt for information
        System.out.print("Customer's name:");
        String name = input.nextLine();

        System.out.print("Customer's address:");
        String address = input.nextLine();

        System.out.print("Customer's phone number:");
        String phone = input.nextLine();

        System.out.print("Customer's licence number:");
        String licence = input.nextLine();

        System.out.print("Customer's credit card number:");
        String ccard = input.nextLine();

        System.out.print("Credit card expiry date (MM/YYYY):");
        String expiry = input.nextLine();

        System.out.print("Hire length (in days):");
        int hire = Integer.parseInt(input.nextLine());

        System.out.print("Make/Model:");
        String model = input.nextLine();

        System.out.print("Registration of car:");
        String rego = input.nextLine();

        System.out.print("Hire rate (per day) Either S, M, L:");
        char inputRate = input.nextLine().charAt(0);

        System.out.print("Total days hired out:");
        int totalDays = Integer.parseInt(input.nextLine());
        //
        double dtotalDays = totalDays;

        double surcharge = dtotalDays - hire;

        //Daily hire rate / Stage 2
        double rate = 0;

        if (inputRate == 'S' || inputRate == 's'){
            rate = (char) 80.0;
            }           
        if (inputRate == 'M' || inputRate == 'm'){
            rate= 110.0;
        }
        if (inputRate == 'L' || inputRate == 'l'){
            rate= 140.0;
        }


        //Calculate late fees
        double penalty = rate * (surcharge * 2);


        //Start Stage 3
        double dCost=0;
        double tCost=0;

        StringBuilder dDetail = new StringBuilder("The damage Details are:" + "\n");
        StringBuilder tDetail = new StringBuilder("The traffic fines are:" + "\n");

        //Setup an exit statement
        boolean quit = false;

        while (!quit){

            System.out.println("<<<Enter one of the following commands:>>>");
            System.out.println("A - Damage Repair");
            System.out.println("B - Traffic Infringement");
            System.out.println("X - Exit Menu");

            String schoiceEntry = input.next();
            char choiceEntry = schoiceEntry.charAt(0);
            //Integer.parseInt(input.nextLine());
            double damageCost = 0;

            switch (choiceEntry){
                case 'A': 
                case 'a':
                        input.nextLine();
                        System.out.println("Enter a description of the damage:"); 
                        String damageDetail = input.nextLine();
                        System.out.println("Enter the damage cost:"); 
                        damageCost = input.nextInt();
                        System.out.print("The damage is: " + damageDetail + "\n");
                        System.out.print("The damage cost is: " + "$" + damageCost + "\n");
                        dDetail.append("-" + damageDetail + "\n");
                        dCost = dCost + damageCost;
                        System.out.println("----------------------------------");
                break;
                case 'B': 
                case 'b':   
                        input.nextLine();
                        System.out.print("Enter a description of the traffic infringement:"); 
                        String trafficDetail = input.nextLine();
                        System.out.println("Enter the traffic infringement cost:");
                        double trafficCost = Integer.parseInt(input.nextLine());
                        tDetail.append("-" + trafficDetail + "\n");
                        tCost = tCost + trafficCost;
                        System.out.println("----------------------------------");
                break;
                case 'X': 
                case 'x':   
                        quit = true; 
                        System.out.print("***Menu entry has been terminated***" + "\n");
                        //System.out.printf("Total traffic cost is: %75s\n", "$" + tCost + "\n");
                        //System.out.printf("Total Damage cost is: %77s\n", "$" + dCost + "\n");
                        //System.out.printf(tDetail + "\n");
                        //System.out.print(dDetail + "\n");
                break;

                default:
                        System.out.print("Please enter either a valid menu selection (A, B, or X):" + "\n");
                break;
            }

        }

        double dhire = hire;
        double charge = dhire*rate;
        double adjustedCharge = charge + penalty;


        //Print summary
        System.out.println("---------------------------------------------"+
                 "------------------------------------------------------\n" +
        "***CUSTOMER DETAILS:***\n");
        System.out.printf("Name: %93s\n", name);
        System.out.printf("Address: %90s\n", address);
        System.out.printf("Phone Number: %85s\n", phone);
        System.out.printf("Licence Number: %83s\n", licence);
        System.out.printf("Credit Card Number: %79s\n", ccard);
        System.out.printf("Credit Card Expiry: %79s\n", expiry);
        System.out.println( "\n***CAR HIRE DETAILS:***\n");
        System.out.printf("Make/Model: %87s\n", model);
        System.out.printf("Registration Number: %78s\n", rego);
        System.out.printf("Hire Length (days): %79s\n", model);
        System.out.printf("Daily Hire Rate: %82s\n", rate);


        System.out.printf("Basic Hire Charge: %80s\n\n", charge);
        System.out.printf("Days hired: %87s\n", totalDays);

        if (totalDays == hire){
        System.out.printf("Late Return Surcharge: %76s\n", "$0.00");
        }
        if (totalDays > hire){
        System.out.printf("Late Return Surcharge: %76s\n", + penalty);
        }




        System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
        System.out.print(dDetail + "\n");
        System.out.printf("Total damage cost is: %78" + "s\n", "$" + dCost + "\n");
        System.out.printf(tDetail + "\n");
        System.out.printf("Total traffic fines incurred: %70s\n", "$" + tCost + "\n");
        System.out.printf("Final hire charge: %79s\n", "$" + (adjustedCharge + dCost + tCost));

    }

}
1
  • If you're going to use printf, you should read up on how it really works, and what the significance of the % signs is. As a general rule, each time you see a % sign in the format string, there should be a matching argument after the format string. In the offending line here, you have just one % in the format string, so only one of the subsequent arguments will be used, and the final argument will be ignored. There are, of course, exceptions to the general rule, but in this case, that's what your problem is. Commented Jun 21, 2015 at 5:17

2 Answers 2

2

I just changed the way you printed out the adjustedCharge so you can still use printf

    System.out.printf("Adjusted Hire Charge: %77s","$");
    System.out.printf("%.1f",adjustedCharge);
    System.out.printf("\n");

With printf you need to format the value you're printing and in this case, because you're trying to print a double, you'll need to format it with %f

I also noticed that my previous answer messed up the spacing you had for the rest of your output. So here's the fixed solution! Just change up the spacing to however you'd like

Sign up to request clarification or add additional context in comments.

1 Comment

Expand on this a bit more; why one has to use f when doing this sort of printing. Also, the cast is unnecessary as adjustedCharge is a double to begin with.
0

Or you can

use Pass two arguments to printf

System.out.printf("Adjusted Hire Charge: %77s\n", "$" + adjustedCharge + "\n")

instead of

 System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.