Computing.Net > Forums > Programming > Help with JAVA-What is the error?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with JAVA-What is the error?

Reply to Message Icon

Name: yelnats126
Date: April 11, 2007 at 21:43:24 Pacific
OS: XP PRO
CPU/Ram: 2G
Product: Dell
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: ravi_kv123
Date: April 12, 2007 at 05:40:13 Pacific
Reply:

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


ravi


0

Response Number 2
Name: sickinthebox
Date: April 15, 2007 at 00:05:20 Pacific
Reply:

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;


0

Response Number 3
Name: sickinthebox
Date: April 15, 2007 at 00:09:17 Pacific
Reply:

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;


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with JAVA-What is the error?

what is the name of the software www.computing.net/answers/programming/what-is-the-name-of-the-software/13798.html

what is the meaning of return 0;? www.computing.net/answers/programming/what-is-the-meaning-of-return-0/5678.html

What is the best way to store data? www.computing.net/answers/programming/what-is-the-best-way-to-store-data/14246.html