0

This is my code:

    import java.util.Scanner;

    public class LabProgram {

    public static double drivingCost(double drivenMiles, double dollarsPerGallon, double milesPerGallon)
    {  double totalCost = 0;
    totalCost = (drivenMiles / milesPerGallon) * dollarsPerGallon ;
    System.out.printf("%.2f", totalCost);
    System.out.print(" ");
    return totalCost;
    }
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double milesPGallon;
    double dollarsPGallon;
    double driveMiles;
    double drivingCost;
    milesPGallon = input.nextDouble();
    dollarsPGallon = input.nextDouble();
    input.close();
    drivingCost(10, dollarsPGallon, milesPGallon);
    drivingCost(50, dollarsPGallon, milesPGallon);
    drivingCost(400, dollarsPGallon, milesPGallon);
    System.out.print("\r");
    }
    }

The output is: '1.58 7.90 63.20 ' and what I need is '1.58 7.90 63.20'. How can I remove the trailing space in the output? I have tried to use trim() and replace() neither has helped at all. I am new to Java and have been banging my head against the wall for the last day and a half trying to figure this out. Any assistance would be appreciated even if it is just a nudge in the correct direction. Thank you in advance.

1 Answers1

2

drivingCost is printing a space every time you call it:

public static double drivingCost(double drivenMiles, double dollarsPerGallon, double milesPerGallon)
{
    double totalCost = 0;
    totalCost = (drivenMiles / milesPerGallon) * dollarsPerGallon ;
    System.out.printf("%.2f", totalCost);
    System.out.print(" "); // <-------- here!
    return totalCost;
}

You can remove that line, and instead print a space between calls to drivingCost:

drivingCost(10, dollarsPGallon, milesPGallon);
System.out.print(" ");
drivingCost(50, dollarsPGallon, milesPGallon);
System.out.print(" ");
drivingCost(400, dollarsPGallon, milesPGallon);

Alternatively, add an extra parameter to drivingCost:

public static double drivingCost(double drivenMiles, double dollarsPerGallon, double milesPerGallon, boolean isLastCall)
{
    double totalCost = 0;
    totalCost = (drivenMiles / milesPerGallon) * dollarsPerGallon ;
    System.out.printf("%.2f", totalCost);

    if (!isLastCall) {
        System.out.print(" ");
    }
    return totalCost;
}

And call it like this:

drivingCost(10, dollarsPGallon, milesPGallon, false);
drivingCost(50, dollarsPGallon, milesPGallon, false);
drivingCost(400, dollarsPGallon, milesPGallon, true);
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 2
    Let's do this right -- The driving cost method shouldn't be printing anything as a method should have a single purpose and should be without side-effects. Rather let the main get the result from drivingCost and then decide how to use it, including printing what it wants. – Hovercraft Full Of Eels Nov 09 '19 at 21:29
  • @HovercraftFullOfEels Well, what if it were called `printDrivingCost`? I assumed (for my own convenience :P) that this is just a badly named method, and this question is not about how to name methods. Though you can argue that `printDrivingCost` shouldn't do the calculation, I think that's out of the scope of this question. Though doing so is nice, We are not expected to fix every single problem (however small) of OP's code, are we? – Sweeper Nov 09 '19 at 21:34
  • ... unless the printing is being used as a "poor-man's debugger" – Hovercraft Full Of Eels Nov 09 '19 at 21:34
  • THank you all for the help. It is now outputting the correct format. I appreciate all the time everyone took. – David bower Nov 13 '19 at 17:51
  • This also works: System.out.printf("%.2f ", totalCost); totalCost = drivingCost(50, milesPerGallon, dollarsPerGallon); System.out.printf("%.2f ", totalCost); totalCost = drivingCost(400, milesPerGallon, dollarsPerGallon); System.out.printf("%.2f", totalCost); System.out.println(); – David bower Nov 14 '19 at 01:56