Hi, I have a problem with a textfield when I try to show the result of a calculation. I tried converting it to String and everything but it says NaN error or infinity error. I better show you some images to help me. the first image shows the jframe code. the second image shows the code of the method that performs the calculation. The problem is that when I enter the data and I give it to calculate, Infinity appears instead of the result in the textfield. I can not find the problem for about 3 days, I would appreciate a solution. I am new here, greetings. Sorry I put images and not the code I did not realize, I do not know very well what is the problem so I do not put the specific question, know apologize. Thanks again.
THIS IS THE SOURCE OF THE NETBEANS JFRAME WHERE THE CODE OF THE JFIELDTEXT AND THE BOTTON IS CALCULATING. moisture calculation package;
public class calculohumedad extends javax.swing.JFrame {
double sueloSeco, sueloHumedo, porcentajeHumedad;
int humedad1;
String humedad;
public calculohumedad() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jtsuelohumedo = new javax.swing.JTextField();
jtsueloseco = new javax.swing.JTextField();
jthumedad = new javax.swing.JTextField();
calcular = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText(" Suelo Humedo");
jLabel2.setText(" Suelo Seco");
jLabel3.setText(" Humedad");
jtsuelohumedo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jtsuelohumedoActionPerformed(evt);
}
});
jtsueloseco.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jtsuelosecoActionPerformed(evt);
}
});
calcular.setText("calcular");
calcular.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calcularActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jtsuelohumedo)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(calcular, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
.addComponent(jtsueloseco))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
.addComponent(jthumedad))))
.addGap(0, 16, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)))
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jtsuelohumedo, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(jtsueloseco)
.addComponent(jthumedad))
.addGap(29, 29, 29)
.addComponent(calcular, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(40, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jtsuelohumedoActionPerformed(java.awt.event.ActionEvent evt) {
sueloHumedo = Double.parseDouble(this.jtsuelohumedo.getText());
}
private void jtsuelosecoActionPerformed(java.awt.event.ActionEvent evt) {
sueloSeco = Double.parseDouble(this.jtsueloseco.getText());
}
private void calcularActionPerformed(java.awt.event.ActionEvent evt) {
calculo micalculo = new calculo();
humedad = String.valueOf(micalculo.calcular(sueloHumedo, sueloSeco));
jthumedad.setText(humedad);
System.out.print(humedad);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new calculohumedad().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton calcular;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jthumedad;
private javax.swing.JTextField jtsuelohumedo;
private javax.swing.JTextField jtsueloseco;
// End of variables declaration
}
AND THIS IS THE CODE OR METHOD OF THE Q CALCULATION IS IN ANOTHER CLASS BUT THE SAME PACKAGE,
package calculationHumidity;
public class calculo {
double sueloHumedo, sueloSeco, porcentajeHumedad;
public double calcular(double sueloHumedo, double sueloSeco){
porcentajeHumedad = ((sueloHumedo-sueloSeco) * 100) / sueloSeco;
return porcentajeHumedad;
}
}