Results 1 to 9 of 9
  1. #1
    tinker99 is offline Member
    Join Date
    Sep 2014
    Posts
    11
    Rep Power
    0

    Default Decimal Formatting Error?

    I am also having trouble with another program.

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.text.DecimalFormat;
    import java.util.StringTokenizer;
    
    public class ShelbyHarms_3_03 {
       public static void main (String [] args) {
       double a, b, c; //Input sides of triangle
       double x; //Perimeter of triangle
       double area; //Area of triangle
       double s; //Semi-Perimeter
       String inputStr;
       StringTokenizer st;
     
       
       //Enter sides of the triangle separated by spaces
       inputStr = JOptionPane.showInputDialog("Enter sides of the triangle separated by spaces");
       st = new StringTokenizer(inputStr);
       a = Double.parseDouble(st.nextToken());
       b = Double.parseDouble(st.nextToken());
       c = Double.parseDouble(st.nextToken());
       
       
       DecimalFormat formatter = new DecimalFormat("#.##");
       
         s = (a + b + c)/2.0;
         
         JOptionPane.showMessageDialog(null, formatter.format(x));
         x = ((s) * (s-a) * (s-b) * (s-c));
          
         JOptionPane.showMessageDialog(null, formatter.format(area));
         area = Math.sqrt(x);
          
       //Output the results
       JOptionPane.showMessageDialog(null,"Your three triangle lengths are: " + '\n' + a + '\n'
                                     + b + '\n' + c + '\n' + "The area of your triangle is: " +
                                     area); 
              
        } // End main()
    } // End class
    Here are the errors:

    ShelbyHarms_3_03.java:39: error: variable x might not have been initialized
    JOptionPane.showMessageDialog(null, formatter.format(x));
    ^
    ShelbyHarms_3_03.java:42: error: variable area might not have been initialized
    JOptionPane.showMessageDialog(null, formatter.format(area));
    ^
    2 errors

    am I not formatting the decimal format right?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Decimal Formatting Error?

    look carefully at lines 28 thru 32 and the error messages. See anything that stands out as odd?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Decimal Formatting Error?

    variable x might not have been initialized
    The compiler likes local variables (defined inside of a method) to have initial values.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    tinker99 is offline Member
    Join Date
    Sep 2014
    Posts
    11
    Rep Power
    0

    Default Re: Decimal Formatting Error?

    It is running now, except when I run the program my formatted area and perimeter end up as 0.00. then it shows my unformatted area.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Decimal Formatting Error?

    Is there a problem with those values?
    What do you expect them to be when they are displayed?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    tinker99 is offline Member
    Join Date
    Sep 2014
    Posts
    11
    Rep Power
    0

    Default Re: Decimal Formatting Error?

    If you are asking if the lengths I am inputting wouldn't work, I am using the same lengths I used from the last triangle program I created, which did work.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Decimal Formatting Error?

    I'm not sure what the problem is?

    Is the results of the computation wrong
    and/or is the value you display wrong ?

    Add a printlin() statement immediately after where the values are computed and print out the values. The output will show you if the computation is wrong.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Decimal Formatting Error?

    Could you please show the values for the lengths of sides of the triangle?

    Edit: Actually, I thought you had fixed the first problem. But initializing the values only
    hid the actual problem. Please look at your original error message and lines 28-32 in the original code
    to see if you notice the problem. It is right there.

    Regards,
    Jim
    Last edited by jim829; 09-29-2014 at 04:53 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    dcarter's Avatar
    dcarter is offline Student
    Join Date
    Sep 2014
    Location
    NC
    Posts
    14
    Rep Power
    0

    Default Re: Decimal Formatting Error?

    Quote Originally Posted by jim829 View Post
    Could you please show the values for the lengths of sides of the triangle?

    Edit: Actually, I thought you had fixed the first problem. But initializing the values only
    hid the actual problem. Please look at your original error message and lines 28-32 in the original code
    to see if you notice the problem. It is right there.

    Regards,
    Jim
    Going off of this, with the JOptionPanes in lines 28-32 you are trying to display the perimeter and then the area, right?

Similar Threads

  1. Decimal.Format error
    By roofninja in forum New To Java
    Replies: 4
    Last Post: 07-09-2012, 11:24 PM
  2. decimal formatting strings
    By dexdyne in forum Advanced Java
    Replies: 14
    Last Post: 09-20-2011, 12:15 PM
  3. Rounding decimal error
    By NixasMuraki in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 01:07 AM
  4. Replies: 3
    Last Post: 01-01-2011, 02:36 AM
  5. get percentage formatting to display 2 decimal places
    By gotenks05 in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 04:57 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
  •