Home > OS >  How to display the float value of a calculation in a text box?
How to display the float value of a calculation in a text box?

Time:04-26

I'm trying to display the 'Day time left' and 'Night time left' after doing a small calculation

I want to be able to select any of the 3 packages from the combo box then when I click calculate it should do the calculation and display the result in the text boxes.

I tried converting a float value to string and then display in the text box, It worked !! so that means this calculation doesen't work.

Somehow the calculation doesn't work so it always displays the value as 0.0

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        jTextPane3.setText(Float.toString(day_left));jTextPane2.setText(Float.toString(night_left));
    } 

I always get 0.0

Always getting 0.0

package slt_package;

public class slt_jframe extends javax.swing.JFrame {
    float tot_day_time;
    float tot_all_time;
    float night_used;
    float day_used;
    float day_left;
    float night_left;
    float night_data;
    
   
    private void package_comboboxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        int selectedPackage = package_combobox.getSelectedIndex();
        
        switch (selectedPackage) {
            case 0:
                tot_day_time = 85;tot_all_time = 205;
                break;
            case 1:
                tot_day_time = 105;tot_all_time = 260;
                break;
            case 2:
                tot_day_time = 190;tot_all_time = 280;
                break;
            default:
                break;
        }
    }                                                

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        day_used=Float.parseFloat(textField1.getSelectedText());   // TODO add your handling code here:
    }                                          

    private void textField2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        night_used=Float.parseFloat(textField2.getSelectedText());
            // TODO add your handling code here:
    }                                          

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        
        jTextPane3.setText(Float.toString(day_left));
        jTextPane2.setText(Float.toString(night_left));
        
    }                                     

}

CodePudding user response:

As Andrew wrote, it's difficult to find the bug without a working program. I would double-check the Float.toString method as seen in Float to String format specifier.

  • Related