This week's book giveaways are in the iOS and Features new in Java 8 forums.
We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Decimal Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Java in General
Bookmark "Decimal Format" Watch "Decimal Format" New topic

Decimal Format

Mindy Truitt

Joined: Sep 07, 2003
Posts: 3
Hi -
After writing the code :
public class Mileage {
public static void main(String[] args) {
DecimalFormat twoDigitsPastPoint = new DecimalFormat("0.00");
double LengthOfTrip, CostOfGas, MilePerGallon, Total;

System.out.print("Enter length of trip: ");
LengthOfTrip = SavitchIn.readLineDouble();
System.out.print("Enter cost of gas: ");
CostOfGas = SavitchIn.readLineDouble();
System.out.print("Enter mile per gallon: ");
MilePerGallon = SavitchIn.readLineDouble();
Total = LengthOfTrip / MilePerGallon * CostOfGas;
System.out.println("The cost of your trip will be: $" + twoDigitsPastPoint.format(Total));
It won't run because of the error:
java.lang.Error: Unresolved compilation problems:
DecimalFormat cannot be resolved or is not a type
DecimalFormat cannot be resolved or is not a type
at Mileage.main(
Exception in thread "main"
I think it has something to do with the 'DecimalFormat' because that is underlined in my program. Any help would be amazing!! Thanks ~ Mindy
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Hi Mindy,
Welcome to JavaRanch!
Strange error messages -- what Java compiler are you using?
Based on just the code you've shown us: including the line
import java.text.DecimalFormat;
at the beginning of the file should fix this problem.

[Jess in Action][AskingGoodQuestions]
Mindy Truitt

Joined: Sep 07, 2003
Posts: 3
I tried that and it got rid of the DecimalFormat error but now the word Format is underlined. I am using Eclipse as the Complier I think. Thanks ~Mindy
Mindy Truitt

Joined: Sep 07, 2003
Posts: 3
Thanks, I got it to work I just had to move the line up a bit.
Thanks, MINDY
I agree. Here's the link:
subject: Decimal Format