Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default double not showing properly?

    Im taking a beginner java course right now and i'm in the process of creating a program. I am trying to format a decimal but i keep getting an error saying you can't format strings. Even though as you see i have the variable set to a double. Any ideas? Here is the code.

    DecimalFormat x = new DecimalFormat("##.##");
    String name = nameInputValue.getText();
    String measure = measureInputValue.getText();

    double height = Double.parseDouble(heightInputValue.getText());
    double mweight = x.format(height * height * 25); // error here saying found: string required: double
    if (measure.equals ("m"))
    {
    outputLabel.setText(height);

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: double not showing properly?

    What Type does DecimalFormat#format(double) return?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    im not even sure how to do what you just suggested. sorry, pretty new.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: double not showing properly?

    Are you aware of the Java API?
    Java Platform SE 6
    Java Platform SE 7

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    im using netbeans IDE 7.0.1 right now if thats what you mean. if you don't then no.

  6. #6
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    still cant figure this out if anyone has an idea. probably will need to be spoon fed.

  7. #7
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: double not showing properly?

    Quote Originally Posted by Reeling View Post
    im using netbeans IDE 7.0.1 right now if thats what you mean. if you don't then no.
    I have no idea about your problem since i am also a beginner.

    As you said you are also pretty new to Java, then it is always advisable that you should not use an IDE like Net-beans, Eclipse because they always add their code since they are already aware of Java.

    You should go with a text editor like Notepad or Notepad++ etc and compile and execute your programs on command line

  8. #8
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: double not showing properly?

    double mweight = x.format(height * height * 25); // error here saying found: string required: double
    This is just a guess work from my side

    Have you tried doing a type-cast here

    i.e.
    Java Code:
    double mweight = (double)x.format(height * height * 25);
    OR

    Java Code:
    String mwght = x.format(height * height * 25);
              double mweight= Double.toParseDouble(mwght);
    Just a guess work.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: double not showing properly?

    Read the API for the version of Java that you are using.

    In the left column, find DecimalFormat

    Find the overload of format(...) that takes a double parameter. Note that it may be in the methods inherited from a superclass.

    Read the method description and the method signature, which tell you what the method returns.

    Does it return a double or something else?

    @Rameshwar: you may be new to Java, but that doesn't mean you can't test code before posting here. And please don't post nonexistent methods that only serve to confuse the issue. Read the API. Even if your code were correct, what sense would it make to convert a double value to a String only to convert it back to a double?

    db
    Last edited by DarrylBurke; 01-30-2012 at 09:36 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    ye that thing rame said didnt work. It removed the error but when i format the decimal to ("##.##") it still shows as 5.0
    I am on java 7 and I have done basically the same thing in a different program and it worked fine, which is why this is so confusing. I know decimalformat is held within java.text because i've used it before. Still very confused.
    Here is a pic of the error:

    Error is here

    idk if you can team view me or something to see if you can figure it out. My very limited knowledge is failing me.
    Last edited by Reeling; 01-30-2012 at 10:10 AM.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: double not showing properly?

    Have you read the API?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    yes, i know it is covered in java.text as i said but i cant understand what the API means when I go to check out the decimalformat part of it.

  13. #13
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    anyone have ideas?

  14. #14
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    still looking, it must be something simple im missing! if anyone wants to team view to take a closer look please let me know.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: double not showing properly?

    Post the relevant entry from the API here.
    That is the entry for DecimalFormat.format().
    That is the signature of the method, then we can walk you through it.
    eg
    Java Code:
    public String getSomething();

  16. #16
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    Quote Originally Posted by Tolls View Post
    Post the relevant entry from the API here.
    That is the entry for DecimalFormat.format().
    That is the signature of the method, then we can walk you through it.
    eg
    Java Code:
    public String getSomething();
    im not even sure, I know im using SE 7. The API is here but like I said before i've done the exact same thing i'm trying to do here before and it worked just fine. It's as though the variable is a double and a string at the same time. Since you can't format strings it errors out so not sure what to do. The API is found here Java Platform SE 7

    I can't really understand what is in the API because i'm really new at this still. here is all the code from the program:


    package idealweight;

    import org.jdesktop.application.Action;
    import org.jdesktop.application.ResourceMap;
    import org.jdesktop.application.SingleFrameApplication;
    import org.jdesktop.application.FrameView;
    import org.jdesktop.application.TaskMonitor;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import javax.swing.Icon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import java.text.*;



    /**
    * The application's main frame.
    */
    public class IdealWeightView extends FrameView {

    public IdealWeightView(SingleFrameApplication app) {
    super(app);

    initComponents();

    // status bar initialization - message timeout, idle icon and busy animation, etc
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout") ;
    messageTimer = new Timer(messageTimeout, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    statusMessageLabel.setText("");
    }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRat e");
    for (int i = 0; i < busyIcons.length; i++) {
    busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
    statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
    }
    });
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
    public void propertyChange(java.beans.PropertyChangeEvent evt) {
    String propertyName = evt.getPropertyName();
    if ("started".equals(propertyName)) {
    if (!busyIconTimer.isRunning()) {
    statusAnimationLabel.setIcon(busyIcons[0]);
    busyIconIndex = 0;
    busyIconTimer.start();
    }
    progressBar.setVisible(true);
    progressBar.setIndeterminate(true);
    } else if ("done".equals(propertyName)) {
    busyIconTimer.stop();
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);
    progressBar.setValue(0);
    } else if ("message".equals(propertyName)) {
    String text = (String)(evt.getNewValue());
    statusMessageLabel.setText((text == null) ? "" : text);
    messageTimer.restart();
    } else if ("progress".equals(propertyName)) {
    int value = (Integer)(evt.getNewValue());
    progressBar.setVisible(true);
    progressBar.setIndeterminate(false);
    progressBar.setValue(value);
    }
    }
    });
    }

    @Action
    public void showAboutBox() {
    if (aboutBox == null) {
    JFrame mainFrame = IdealWeightApp.getApplication().getMainFrame();
    aboutBox = new IdealWeightAboutBox(mainFrame);
    aboutBox.setLocationRelativeTo(mainFrame);
    }
    IdealWeightApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    mainPanel = new javax.swing.JPanel();
    titleLabel = new javax.swing.JLabel();
    nameInputLabel = new javax.swing.JLabel();
    measureInputLabel1 = new javax.swing.JLabel();
    measureInputLabel2 = new javax.swing.JLabel();
    heightInputLabel1 = new javax.swing.JLabel();
    heightInputLabel2 = new javax.swing.JLabel();
    nameInputValue = new javax.swing.JTextField();
    measureInputValue = new javax.swing.JTextField();
    heightInputValue = new javax.swing.JTextField();
    outputButton = new javax.swing.JButton();
    outputLabel = new javax.swing.JLabel();
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();

    mainPanel.setName("mainPanel"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(i dealweight.IdealWeightApp.class).getContext().getR esourceMap(IdealWeightView.class);
    titleLabel.setFont(resourceMap.getFont("titleLabel .font")); // NOI18N
    titleLabel.setForeground(resourceMap.getColor("tit leLabel.foreground")); // NOI18N
    titleLabel.setText(resourceMap.getString("titleLab el.text")); // NOI18N
    titleLabel.setName("titleLabel"); // NOI18N

    nameInputLabel.setFont(resourceMap.getFont("nameIn putLabel.font")); // NOI18N
    nameInputLabel.setText(resourceMap.getString("name InputLabel.text")); // NOI18N
    nameInputLabel.setName("nameInputLabel"); // NOI18N

    measureInputLabel1.setFont(resourceMap.getFont("me asureInputLabel1.font")); // NOI18N
    measureInputLabel1.setText(resourceMap.getString(" measureInputLabel1.text")); // NOI18N
    measureInputLabel1.setName("measureInputLabel1"); // NOI18N

    measureInputLabel2.setFont(resourceMap.getFont("me asureInputLabel2.font")); // NOI18N
    measureInputLabel2.setText(resourceMap.getString(" measureInputLabel2.text")); // NOI18N
    measureInputLabel2.setName("measureInputLabel2"); // NOI18N

    heightInputLabel1.setFont(resourceMap.getFont("hei ghtInputLabel1.font")); // NOI18N
    heightInputLabel1.setText(resourceMap.getString("h eightInputLabel1.text")); // NOI18N
    heightInputLabel1.setName("heightInputLabel1"); // NOI18N

    heightInputLabel2.setFont(resourceMap.getFont("hei ghtInputLabel2.font")); // NOI18N
    heightInputLabel2.setText(resourceMap.getString("h eightInputLabel2.text")); // NOI18N
    heightInputLabel2.setName("heightInputLabel2"); // NOI18N

    nameInputValue.setFont(resourceMap.getFont("nameIn putValue.font")); // NOI18N
    nameInputValue.setText(resourceMap.getString("name InputValue.text")); // NOI18N
    nameInputValue.setName("nameInputValue"); // NOI18N

    measureInputValue.setFont(resourceMap.getFont("mea sureInputValue.font")); // NOI18N
    measureInputValue.setName("measureInputValue"); // NOI18N

    heightInputValue.setFont(resourceMap.getFont("heig htInputValue.font")); // NOI18N
    heightInputValue.setName("heightInputValue"); // NOI18N

    outputButton.setText(resourceMap.getString("output Button.text")); // NOI18N
    outputButton.setName("outputButton"); // NOI18N
    outputButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    outputButtonActionPerformed(evt);
    }
    });

    outputLabel.setFont(resourceMap.getFont("outputLab el.font")); // NOI18N
    outputLabel.setForeground(resourceMap.getColor("ou tputLabel.foreground")); // NOI18N
    outputLabel.setText(resourceMap.getString("outputL abel.text")); // NOI18N
    outputLabel.setName("outputLabel"); // NOI18N

    javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
    mainPanelLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addGap(39, 39, 39)
    .addComponent(titleLabel)
    .addContainerGap(45, Short.MAX_VALUE))
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addGap(48, 48, 48)
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING)
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addComponent(measureInputLabel2)
    .addContainerGap())
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING)
    .addComponent(nameInputLabel)
    .addComponent(heightInputLabel1)
    .addComponent(heightInputLabel2)
    .addComponent(measureInputLabel1))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 90, Short.MAX_VALUE)
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING)
    .addComponent(outputButton)
    .addComponent(nameInputValue, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(heightInputValue, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(measureInputValue, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(86, Short.MAX_VALUE))
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())))
    );
    mainPanelLayout.setVerticalGroup(
    mainPanelLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addContainerGap()
    .addComponent(titleLabel)
    .addGap(18, 18, 18)
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(nameInputLabel)
    .addComponent(nameInputValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(59, 59, 59)
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(measureInputLabel1)
    .addComponent(measureInputValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(measureInputLabel2)
    .addGap(73, 73, 73)
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(heightInputLabel1)
    .addComponent(heightInputValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(heightInputLabel2)
    .addGap(18, 18, 18)
    .addComponent(outputButton)
    .addGap(18, 18, 18)
    .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(26, Short.MAX_VALUE))
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.t ext")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(i dealweight.IdealWeightApp.class).getContext().getA ctionMap(IdealWeightView.class, this);
    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.t ext")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBo x")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator "); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax. swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel "); // NOI18N

    progressBar.setName("progressBar"); // NOI18N

    javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
    statusPanelLayout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING)
    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
    .addGroup(statusPanelLayout.createSequentialGroup( )
    .addContainerGap()
    .addComponent(statusMessageLabel)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 272, Short.MAX_VALUE)
    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(statusAnimationLabel)
    .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
    statusPanelLayout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING)
    .addGroup(statusPanelLayout.createSequentialGroup( )
    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addGroup(statusPanelLayout.createParallelGroup(ja vax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(statusMessageLabel)
    .addComponent(statusAnimationLabel)
    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(3, 3, 3))
    );

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
    }// </editor-fold>

    private void outputButtonActionPerformed(java.awt.event.ActionE vent evt) {
    DecimalFormat x = new DecimalFormat("##.00");
    String name = nameInputValue.getText();
    String measure = measureInputValue.getText();
    double height = Double.parseDouble(heightInputValue.getText()); // Error here saying "incompatible types Required: double Found: java.lang.String
    double weight = x.format(height * height * 25);



    if (measure.equals ("m"))
    {
    outputLabel.setText (Double.toString(weight));

    }

    }


    // Variables declaration - do not modify
    private javax.swing.JLabel heightInputLabel1;
    private javax.swing.JLabel heightInputLabel2;
    private javax.swing.JTextField heightInputValue;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel measureInputLabel1;
    private javax.swing.JLabel measureInputLabel2;
    private javax.swing.JTextField measureInputValue;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel nameInputLabel;
    private javax.swing.JTextField nameInputValue;
    private javax.swing.JButton outputButton;
    private javax.swing.JLabel outputLabel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    }

    I'm so confused as to why this wont work!!!!
    btw the inputvalues it is trying to get the text from are all blank text fields
    Last edited by Reeling; 02-01-2012 at 08:52 AM.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: double not showing properly?

    It's this line that has the error (not the one you're highlighting):
    Java Code:
    double weight = x.format(height * height * 25);
    (and please use code tags).

    That could never have worked in any version of Java.
    Look at the API for Double.format().
    What does it say it returns!

  18. #18
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    like i said i cant really read the API but I think it says it returns -0.0. Yes i accidentally put the comment on the wrong line. If I can't format a double how can i make it so the number shows up with no decimals? I need to be able to accept a number with decimals as the value for height but then I want to get rid of the decimals for the value of weight.


    Here is an example of what I did on a different program which looks to me, to be the exact same thing. *Maybe I am mistaken?

    Java Code:
    private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // Declare variables that user enters for diameter and to hold value of 
        // answer for calculation of cost
        // Set decimal format to two places with a $ in front
        double diam,cost;
        DecimalFormat x = new DecimalFormat("$##.00");
        
        // getText is used to get value that user entered then use
        // parseDouble to convert to a number
        diam = Double.parseDouble(diamInput.getText());
        cost = 1 + 1.50 + (0.5*diam);
        
        // use setText to display the cost of the pizza in the outputlabel
        outputLabel.setText("The cost of the pizza is "+ x.format(cost));
    }
    Last edited by Reeling; 02-01-2012 at 10:47 PM.

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: double not showing properly?

    Quote Originally Posted by Tolls View Post
    Look at the API for Double.format().
    That should be DecimalFormat.format(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  20. #20
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: double not showing properly?

    okay i'm not sure what you guys are tying to do here. not to be rude but I came here looking for an answer, not to be shown resources of how I can figure the answer out on my own. I have been trying to do that for the past 3 days. I can't figure it out. If anyone can just simply tell me how I can format the weight variable to have no decimals that'd be great. Thanks.

Page 1 of 2 12 LastLast

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  2. JTextField not showing properly in GridBagLayout
    By aserothbw in forum AWT / Swing
    Replies: 13
    Last Post: 10-02-2009, 12:11 PM
  3. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 03:51 PM
  4. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

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