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.