Issue
I want to know how can i access the local variable AverageBodylengths
in another methord call jButton4ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
float num3 = Integer.parseInt(jTextField5.getText());
float num4 = Integer.parseInt(jTextField6.getText());
float num5 = Integer.parseInt(jTextField7.getText());
float num6 = Integer.parseInt(jTextField8.getText());
float num7 = Integer.parseInt(jTextField9.getText());
float num8 = Integer.parseInt(jTextField10.getText());
float num9 = Integer.parseInt(jTextField11.getText());
float num10 = Integer.parseInt(jTextField12.getText());
float num11 = Integer.parseInt(jTextField13.getText());
float num12 = Integer.parseInt(jTextField14.getText());
float AverageBodylengths = (((num3) + (num4) + (num5) + (num6) + (num7) + (num8) + (num9) + (num10) + (num11) + (num12)) / 10);
jLabel25.setText("" + AverageBodylengths + "mm");
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
}
Solution
Make the variable global. Your code would look like this:
private float averageBodyLenght;
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
float num3 = Integer.parseInt(jTextField5.getText());
float num4 = Integer.parseInt(jTextField6.getText());
float num5 = Integer.parseInt(jTextField7.getText());
float num6 = Integer.parseInt(jTextField8.getText());
float num7 = Integer.parseInt(jTextField9.getText());
float num8 = Integer.parseInt(jTextField10.getText());
float num9 = Integer.parseInt(jTextField11.getText());
float num10 = Integer.parseInt(jTextField12.getText());
float num11 = Integer.parseInt(jTextField13.getText());
float num12 = Integer.parseInt(jTextField14.getText());
averageBodylengths = (((num3) + (num4) + (num5) + (num6) + (num7) + (num8) + (num9) + (num10) + (num11) + (num12)) / 10);
jLabel25.setText("" + AverageBodylengths + "mm");
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//Do stuff with averageBodyLenght
}
Answered By - Nikita Demodov
Answer Checked By - Mary Flores (JavaFixing Volunteer)