When I run this I get an error, it's like it's not reading the printf and honestly I have no idea what else to do, can someone help me?

import java.util.Scanner;
public class MyCarPool2 {
public static void main (String [] args){
      final int WEEK = 5,
                MONTH = 22,
                YEAR = 260;
        int mpd, //miles driven per day
          cpg, //cost per gallon of gas
          mpg, // miles per gallon
          fpd, // parking fees per day
          tpd, // tolls per day
          nop; // number of passengers
      double dailyCost,
   Scanner input = new Scanner (System.in);
   java.util.Date dateCreated = new java.util.Date();

      //Reading data
      System.out.print ("Total miles driven per day:\t");
        mpd = input.nextInt();
        System.out.print ("Cost per gallon of gasoline:\t");
        cpg = input.nextInt();
        System.out.print ("Average miles per gallon:\t");
        mpg = input.nextInt();
      System.out.print ("Parking fees per day:\t");
        fpd = input.nextInt();
      System.out.print ("Toll cost per day:\t");
        tpd = input.nextInt();
      System.out.print ("Number of passengers:\t");
        nop = input.nextInt();

      //Compute the totals
      dailyCost = (mpd * mpg / cpg) + fpd + tpd;
      dailySavings = dailyCost - (dailyCost / nop);
      weeklySavings = dailySavings * WEEK;
      monthlySavings = dailySavings * MONTH;
      annualSavings = dailySavings * YEAR;

      System.out.println ("\n\t***************** Car Pooling Report ****************");
      System.out.printf  ("\n\t%-30s%-20s","Name:","Tierra Christian");
      System.out.printf  ("\n\t%-30s%-20s","Course:","CS1301B");
      System.out.printf  ("\n\t%-30s%-20s","Date Created:",dateCreated);
      System.out.println ("\n\t*****************************************************");
      System.out.printf  ("\n\n\t%-30s%20d""One Passenger Daily Cost:",dailyCost);
      System.out.printf  ("\n\n\t%-30s%20d","Daily Savings:",dailySavings);
      System.out.printf  ("\n\n\t%-30s%20d","Weekly Savings:",weeklySavings);
      System.out.printf  ("\n\n\t%-30s%20d","Monthly Savings:",monthlySavings);
      System.out.printf  ("\n\n\t%-30s%20d","Annual Savings:",annualSavings);
      System.out.println ("*************** Have a nice day *************************");

Recommended Answers

All 3 Replies

Alright so I figured it out, but it won't let me put in decimals, like 3.45 dollars per gallon - it just says error.

If printing is not working try to import java.io.*;

The cost per gallon is an integer, so decimals won't work for any integer value where you need to input a fractional value. Use doubles for those as well.

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.