Results 1 to 3 of 3
  1. #1
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default Help with rounding decimals

    I need some help! I can't seem to get my output to round to two decimal places. Can anyone give me a hand with this I have tried countless ways of doing this and none of them have worked. I thought the code - DecimalFormat fmt = new DecimalFormat ("0.##"); would work but it hasn't.

    Here is my program-

    import java.awt.Color;
    import java.awt.Dimension;
    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    import java.util.Scanner;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;


    public class CarTracker {

    //This declares Variables

    private String firstName, lastName;
    private double fares;
    private double fuelUsed, distanceDriven;

    public CarTracker(String first, String last) {
    firstName = first;
    lastName = last;
    fares = 0.0;
    fuelUsed = 0.0;
    distanceDriven = 0.0;
    }

    //This sets up all the variables that we will be using and uses them as a string or as a double varaible.

    public String driversName() {
    return (firstName + ' ' + lastName);
    }

    public void addFare(double fare) {
    fares += Math.abs(fare);
    }

    public void addFuel(double fuel) {
    fuelUsed += Math.abs(fuel);
    }

    public void drive(double distance) {
    distanceDriven += Math.abs(distance);
    }

    public double readMeter() {
    return fares;
    }

    public double readFuelGage() {
    return fuelUsed;
    }

    public double readOdometer() {
    return distanceDriven;
    }

    public double fuelEfficency() {
    return (fuelUsed / distanceDriven);
    }

    public double fareRate() {
    return (fares / distanceDriven);
    }

    public double mileageCost(double fuelPurchased) {
    return (fuelPurchased / fuelUsed);
    }

    public double Profit() {
    return (fares - fuelUsed);
    }

    public static void main(String[ ] args ) {

    Scanner reader;

    reader = new Scanner( System.in );

    //This is where the program will ask the user to enter values for the questions posted.

    System.out.println("Enter the driver's first name:");
    String firstName = reader.nextLine();

    System.out.println("Enter the driver's last name:");
    String lastName = reader.nextLine();

    CarTracker driver = new CarTracker(firstName, lastName);

    System.out.println("Enter the amount paid to the driver:");
    double fares = reader.nextDouble();
    driver.addFare(fares);

    System.out.println("Enter the amount the driver paid for gas:");
    double gasPrice = reader.nextDouble();

    System.out.println("Enter the distance the driver traveled:");
    double distance = reader.nextDouble();
    driver.drive(distance);

    System.out.println("Please enter the amount of gasoline the Driver used:");
    double fuel = reader.nextDouble();

    // Presents two colored panels nested within a third.

    JFrame frame = new JFrame ("Nested Panels");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    JPanel primary = new JPanel();
    primary.setPreferredSize (new Dimension(250, 250));
    primary.setBackground (Color.green);

    JLabel label1 = new JLabel ("Name: " + driver.driversName());
    JLabel label2 = new JLabel ("Miles Per Gallon: = " + (distance/fuel));
    JLabel label3 = new JLabel ("Fares Per Mile: = " + (fares/distance));
    JLabel label4 = new JLabel ("Gas Paid Per Mile: = " + (gasPrice/distance));
    JLabel label5 = new JLabel ("Total Profit: = " + (fares-gasPrice));

    primary.add (label1);
    primary.add (label2);
    primary.add (label3);
    primary.add (label4);
    primary.add (label5);

    frame.getContentPane().add(primary);
    frame.pack();
    frame.setVisible(true);
    }
    }

  2. #2
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Actually I figured that out. Now I just need to do some mutation to my output. When I type in john betterDRIVER in I need the output to say John BetterDriver. Can anyone help?

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Well, firstly, you'd want to convert your entire string to lowercase, using .toLowerCase().

    For the words "John" and "Better", you can capitalize them by finding the first letter in the string and capitalizing it, then finding the index of the first space, and capitalizing the letter after it. Familiarize yourself with .indexOf() and .toUpperCase().

    For the word "Driver", that's totally up in the air as it appears to be arbitrarily selected. Unless you have a method to select this word from the string and capitalize it, the computer can't legitimately tell that the words "Better" and "Driver" are separate.

Similar Threads

  1. BIG decimals
    By xael in forum New To Java
    Replies: 17
    Last Post: 10-04-2010, 10:14 AM
  2. Formatting numbers or decimals (around comma)
    By Joris in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2010, 05:46 PM
  3. Decimals in java help!!
    By Gold in forum New To Java
    Replies: 3
    Last Post: 12-09-2009, 11:25 PM
  4. number to word with decimals
    By fertas in forum New To Java
    Replies: 3
    Last Post: 08-30-2008, 03:12 AM
  5. get more decimals?!?! please help!
    By michcio in forum New To Java
    Replies: 7
    Last Post: 05-22-2008, 10:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •