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 );

	}

}

Dani AI

Generated

The behavior seen in this thread has two separate causes: a type mismatch (which led to the compile error) and string concatenation turning arithmetic into text (which produced the "17.7...5.50.35" output). was correct to change the numeric variables to floating-point, and correctly pointed out that once Java starts concatenating with a String the rest of the + operations append as text. A safe rule: do the math first (store it in a numeric variable) and format the result when printing.

If the goal is correct money arithmetic, avoid relying on raw double for currency values. BigDecimal gives predictable rounding and exact decimal arithmetic. Example pattern:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

BigDecimal distance = new BigDecimal("200");
BigDecimal mpg = new BigDecimal("31.5");
BigDecimal ppg = new BigDecimal("2.79");
BigDecimal parking = new BigDecimal("5.50");
BigDecimal tolls = new BigDecimal("0.35");

BigDecimal gallons = distance.divide(mpg, 10, RoundingMode.HALF_UP);
BigDecimal fuelCost = gallons.multiply(ppg);
BigDecimal total = fuelCost.add(parking).add(tolls).setScale(2, RoundingMode.HALF_UP);

System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(total));

For quick work with doubles, compute into a numeric total first and then format at print time to control decimals and avoid concatenation surprises. Also watch out for integer division — if both operands are integers you will get truncated division; use a floating literal (e.g., 200.0) or cast to double.

Quick checklist:

  • Remove unused Scanner if no input is read (saves confusion).
  • Ensure at least one operand is floating-point to avoid integer division.
  • Compute arithmetic into a numeric variable before concatenating.
  • Use BigDecimal for currency or use NumberFormat/Formatter for display formatting (see BigDecimal docs and NumberFormat docs for details). References: BigDecimal API, NumberFormat API.

Mentions: , , , , .

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.