Java DecimalFormat class

118 views Asked by At

I'm a complete beginner in Java and having a very difficult time importing the DecimalFormat class right now.

import java.text.DecimalFormat;

public class DecimalFormatDemo {
    public static void main(String[] args) {
        DecimalFormat formatter1 = new DecimalFormat("0.0");
        DecimalFormat formatter2 = new DecimalFormat("00.00");
        DecimalFormat formatter3 = new DecimalFormat(".00");
        DecimalFormat formatter4 = new DecimalFormat("0.00%");
 
        System.out.println("0.0: " + formatter1.format(.8675309));
        System.out.println("00.00: " + formatter2.format(.8675309));
        System.out.println(".00: " + formatter3.format(.8675309));
        System.out.println("0.00%: " + formatter4.format(.8675309));
        System.out.println(".00: " + formatter3.format(8675309));
    }
}

Whenever I attempt to run this code my compiler gives the error:

"[{
    "resource": "/Java OOP I/DecimalFormatDemo.java",
    "owner": "_generated_diagnostic_collection_name_#2",
    "code": "268435846",
    "severity": 8,
    "message": "The import java.text.DecimalFormat cannot be resolved",
    "source": "Java",
    "startLineNumber": 1,
    "startColumn": 8,
    "endLineNumber": 1,
    "endColumn": 31
}]

Currently, I'm running on Java version 21.0.1 LTS. From my understanding this version still supports DecimalFormat, but I am not sure why it doesn't recognize this import. I'm using VS code on MacOS and downloaded code.visualstudio.com/docs/languages/java to help run Java in the IDE. When I push the play button in the top right corner it gives this error code. Any help would be very much appreciated!

Tried looking to see if I was missing the class, if there was any spelling mistakes, or if this has been posted before and was not able to find any support on this question.

1

There are 1 answers

0
Mark Karman On

The issue has been resolved. After installing the "Extension Pack for Java", pointing Java to the correct directory, and restarting my IDE I was able to correctly run this code in VS code with no issues.