Results 1 to 5 of 5
  1. #1
    blue crystal is offline Member
    Join Date
    May 2015
    Posts
    2
    Rep Power
    0

    Default Program cannot see DECIMAL FORMAT

    I am using NetBeans IDE 8.0.2 to program Java code. I am a beginner in Java.

    Instead of getting two decimal places after the point, I am getting three despite using the code format for two ("##.##"). Actually, this happens even if I remove the format code between the quotes. It is as if the program cannot see the format code. Why this happens ?
    Here is the relevant program code:

    private void convertButtonActionPerformed(java.awt.event.Action Event evt) {
    double inputNumber = 0;
    // sets the decimal format
    // I had to create a GUI class in order to work
    DecimalFormat df = new DecimaFormat("##.##");
    // get the user imput
    try {
    inputNumber = Double.parseDouble(milesTextField.getText());
    }
    catch( Exception e ){
    JOptionPane.showMessageDialog( this, "Please enter a valid number", "Error", JOptionPane.ERROR_MESSAGE );
    }
    // calculate and set the answer
    String answer = df.format( inputNumber * 1.609344f );
    this.kilometerLabel.setText(answer + " kilometers");
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Program cannot see DECIMAL FORMAT

    What you posted won't compile because of the typo ("DecimaFormat" for "DecimalFormat").

    It might be a good idea to post a brief runnable example together with a brief description of what you observe and what you were expecting to see.

    Java Code:
    pbrockway@pbrockway-OptiPlex-980:~/Desktop$ cat DfTest.java
    import java.text.DecimalFormat;
    
    public class DfTest {
        public static void main(String[] args) {
            DecimalFormat df = new DecimalFormat("##.##");
            double inputNumber = 10.0;
             
            String result = df.format(inputNumber * 1.609344);
            System.out.println(result);
        }
    }
    pbrockway@pbrockway-OptiPlex-980:~/Desktop$ javac DfTest.java 
    pbrockway@pbrockway-OptiPlex-980:~/Desktop$ java DfTest 
    16.09
    pbrockway@pbrockway-OptiPlex-980:~/Desktop$
    As you can see this code does produce 2 decimal places as expected.

  3. #3
    blue crystal is offline Member
    Join Date
    May 2015
    Posts
    2
    Rep Power
    0

    Default Re: Program cannot see DECIMAL FORMAT

    Hi pbrockway2

    I thought NetBeans would point out spelling errors.

    Thank you very much !

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Program cannot see DECIMAL FORMAT

    I'm glad you got it sorted out.

    I would have imagined there would be a squiggly line and, if you hover somewhere, you're told "unknown symbol" or "unknown type" or something. One annoying feature of NB is to run code when there are compiler errors. As you've seen this really doesn't help because whatever code it is that is running, it's not the code you want.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Program cannot see DECIMAL FORMAT

    Quote Originally Posted by blue crystal View Post
    Hi pbrockway2

    I thought NetBeans would point out spelling errors.
    No, but it does point out compile errors. Often referred to as "red squigly lines". If you are not seeing them, then you are not using Netbeans as you should.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Need program to print in decimal format
    By Ross92 in forum New To Java
    Replies: 6
    Last Post: 10-25-2014, 06:01 PM
  2. Decimal.Format error
    By roofninja in forum New To Java
    Replies: 4
    Last Post: 07-09-2012, 11:24 PM
  3. How do you format to two decimal places with println()?
    By EscSequenceAlpha in forum New To Java
    Replies: 4
    Last Post: 04-05-2012, 08:27 PM
  4. Can't get the decimal format to format correctly...
    By Valerie1067 in forum New To Java
    Replies: 5
    Last Post: 03-25-2012, 04:15 AM
  5. Replies: 7
    Last Post: 09-14-2011, 03:06 PM

Tags for this Thread

Posting Permissions

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