Computing.Net > Forums > Programming > need help w/ java programming

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.

need help w/ java programming

Reply to Message Icon

Name: j3r3my_07
Date: February 21, 2004 at 16:07:16 Pacific
OS: xp pro
CPU/Ram: xp1500/1gb
Comment:

Hi all,

Im taking course intro to java programming and Im having problem w/ my program. I suppose to get an average number from three input number but I dont get the right result. For instance, i put 5,7, and 11 in the textField and the result is 15 and I know it's not right, cuz it's supposed to be 7...and this is the compile codes I have:

// calculate the average of the three values
private void calculateJButtonActionPerformed( ActionEvent event )
{
// get values input by user
int number1 = Integer.parseInt(
firstNumberJTextField.getText() );
int number2 = Integer.parseInt(
secondNumberJTextField.getText() );
int number3 = Integer.parseInt(
thirdNumberJTextField.getText() );

// sum number1, number2, and number3 and divide by 3
int result = number1 + number2 + number3 / 3;

// display the result
averageJTextField.setText( String.valueOf( result ) );

} // end method calculateJButtonActionPerformed

I might put this wrong, but if someone out there who can help i'd be very appreciated...

thanks,
j3r3my07



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: February 21, 2004 at 16:19:31 Pacific
Reply:

You need to look into something called "Operator Precedence". The rules of Operator Precedence control which order operators are evaluated when no paranthese are used.

They are basically the same in most programming languages as they are in basic math.

What is: 2 + 4 / 2 ?

3? or 4?

Is it:

(2 + 4) / 2 ?

or

2 + (4 / 2) ?


Basically, you need some parantheses in your calculation line.


0

Response Number 2
Name: j3r3my_07
Date: February 21, 2004 at 16:26:03 Pacific
Reply:

OMG what was I thinking...yah i guess i've must forgot about the parantheses...im gonna give it a try then...i'll post back later...

thanks man,
j3r3my


0

Response Number 3
Name: j3r3my_07
Date: February 21, 2004 at 16:29:02 Pacific
Reply:

IT WORKS....thanks Don...really appreciated it :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


web server connection I would like to learn C++...



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: need help w/ java programming

Help on java program www.computing.net/answers/programming/help-on-java-program/7078.html

I need help in Java www.computing.net/answers/programming/i-need-help-in-java/7027.html

need help w/ java programming www.computing.net/answers/programming/need-help-w-java-programming/9635.html