Hello everyone. I am new to java and am stuck on a problem i need to complete. The problem asks for me to create a program that calculates daily driving cost. The application needs to have total miles driven, cost per gallon, mpg, parking fees and tolls. Now, my professor doesnt like to teach us anything but still expects us to know everyting so i have been struggling a little bit. i have determined i need to use "double" instead of "int"....but when i go to do the math for this it gives me syntax error. How would i go about doing math using the double instead of int? here is what i have so far. would really appreciate it if sombody could help me out. The data that needs to be used is in my code. I also need to print the results using printf, which im a little lost on.

import java.util.Scanner;
public class carpoolsavings {

	public static void main(String[] args) {
		
		Scanner input = new Scanner (System.in);
		
		int total;
		int distance = 200;
		double ppg = 2.79;
		double parking = 5.50;
		double tolls = .35;
		double mpg = 31.5;
		
		
		total = (distance / mpg * ppg + parking + tolls);

		System.out.printf("Total Price is " + total );

	}

}

Recommended Answers

All 7 Replies

cant anybody point me in the right direction?

I would try:
Make all of your variables of type double. Use println for output.

double total;
		double distance = 200.00;
		double ppg = 2.79;
		double parking = 5.50;
		double tolls = .35;
		double mpg = 31.5;
		System.out.println("Total Price is " + total );

Let me know if that works.

yes thank you, that works somewhat! here is my new revised code but it still has somthing wrong with it.

import java.util.Scanner;
public class carpoolsavings {

	public static void main(String[] args) {
		
		Scanner input = new Scanner (System.in);
		
		      double distance = 200.00;
		      double ppg = 2.79;	   
		      double parking = 5.50;
		      double tolls = .35;
		      double mpg = 31.5;

		      System.out.println("Total Price is $" + distance/mpg*ppg+parking+tolls );


	}

}

my output is:

Total Price is $17.7142857142857155.50.35

notice how its just printing the values for tolls and parking instead of adding them, and also how there are so many decimal places. what is wrong with my math line? do i need to add some parenthesis somwhere?

thanks alot.

I'm not sure and Im not around a compiler right now. I would try breaking it up like you originally had. Try this:

import java.util.Scanner;
public class carpoolsavings {

	public static void main(String[] args) {
		
		double total;
		double distance = 200.00;
		double ppg = 2.79;
		double parking = 5.50;
		double tolls = .35;
		double mpg = 31.5;
		total = (distance / mpg * ppg + parking + tolls);
		System.out.println("Total Price is " + total );
	}
}

Also you don't need any scanner input so you can take that line out.

there is no need for scanner if u r setting your values already. Also, u can use this to reduce the number of decimal places

public static void main (String [] args){
    DecimalFormat twoDigit = new DecimalFormat("#,##0.000");//formats to 3 decimal places
    DecimalFormat twoDigit = new DecimalFormat("#,##0.00");//formats to 2 decimal places
    DecimalFormat oneDigit = new DecimalFormat("#,##0.0");//format to 1 decimal place
    /**
     *can format to any decimal place, just by editting the "zeros"("#,##0.000"),
     *and changing to a meaningful varible name(threeDigit)
     */

      System.out.println(twoDigit.format(anyVariable));
      System.out.println(oneDigit.format(anyVariable));

The problem you faced earlier with:

System.out.println("Total Price is $" + distance/mpg*ppg+parking+tolls );

was because "+" is a String concatenation operator. Do your math operations, store the value in a variable, and then print the variable.

To use printf you can do the following:

double total;

total = distance / mpg * ppg + parking + tolls;

//print to 2 decimal places
//for 3 decimal places, use "%.3f"
System.out.printf("Total Price is %.2f \n", total );

total = distance / mpg * ppg + parking + tolls;

or

total = distance / ( mpg * ppg + parking + tolls ); ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.