Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
import javax.swing.*;
import java.awt.event.*;public class Debugging3MaS extends JApplet
{
//Declare components
JLabel programmerLabel = new JLabel("Programmed by Your Name");
JTextField numberTextField = new JTextField(5);
JPanel mainPanel = new JPanel();
JButton pressMeButton = new JButton("Press Me");
JTextArea outputTextArea = new JTextArea(3, 30);//Declare global variables
float answerFloat;
float numberFloat;public void init()
{
setContentPane(mainPanel);
mainPanel.add(programmerLabel);
mainPanel.add(new JLabel("Enter a whole number: "));
mainPanel.add(numberTextField);
mainPanel.add(pressMeButton);
mainPanel.add(outputTextArea);
numberTextField.requestFocus();
pressMeButton.addActionListener(this);
}public void actionPerformed(ActionEvent event)
{
//Multiply the number entered by 2 and display the result
numberFloat = numberTextField.getText();
calculateAnswer();
outputTextArea.setText("The number doubled is " + answerFloat);
}
public void calculateAnswer()
{
//calculate 2 times the entered number
float answerFloat = 0;
answerFloat = numberFloat * 2;}
}Stanley

whts the output actually...???
i think....
outputTextArea.setText("The number doubled is " + answerFloat);
keep tht output in calculateAnswer() function and try...
ravi

For one, the line...
numberFloat = numberTextField.getText();
attemps to put a String into a float, you'll have to write an algorythm that converts the String to a float, or use some prewritten function that I don't know about.Also, you're overloading the answerFloat variable in caluculateAnswer(), so it is always returning 0. Remove the line,
float answerFloat = 0; in calculateAnswer().I wouldn't recomend using global variables for this program, just pass and return values.
~Ben;

Sorry, some corrections...
You are re-declaring answerFloat in the function, so it is loosing scope once it leaves that function,
but still remove the line
float answerFloat = 0; in calculateAnswer()~Ben;

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |