Richard Berry

Greenhorn
+ Follow
since Jan 18, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Richard Berry

OK, thanks. That answers the why.

Your answers helped me to know what to look for in a web search. Here is a good explanation with some suggestions of how to get around the problem:

13 years ago
I am not a total beginner at programming, but I am a total newbie with Java. I followed this tutorial http://netbeans.org/kb/docs/java/gui-functionality.html#Exercise_1, and had no problem doing the exercise, but the results are confusing me.

The attached screenshot shows an error of .000002 when adding 12.6 and 9.3.

Some results compute perfectly though. 11.25 + 13.5 gives 24.75, but 111.3 + 50.9 gives 162.20001

The relevant code is:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// First we define float variables.
float num1, num2, result;
// We have to parse the text to a type float.
num1 = Float.parseFloat(jTextField1.getText());
num2 = Float.parseFloat(jTextField2.getText());
// Now we can perform the addition.
result = num1+num2;
// We will now pass the value of result to jTextField3.
// At the same time, we are going to
// change the value of result from a float to a string.
jTextField3.setText(String.valueOf(result));
}

I'm happy to post all my source code if required, though I have not deviated from the exercise except that my buttons 1 and 2 are swapped around, but I cannot see how that would matter.

I would be very appreciative of any assistance.
13 years ago