wood burning stoves*
The moose likes Beginning Java and the fly likes If-Else help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If-Else help please" Watch "If-Else help please" New topic
Author

If-Else help please

T Bart
Greenhorn

Joined: Apr 17, 2011
Posts: 5

Can someone tell me why this if-else statement will still allow values over 240 to be entered into my array? This is from a small project with a Swing GUI that allowsa user to enter a value into jTextArea1. When the user hits a button it evaluates the value using the below statement. If the user enters a value of 240 or greater, the message in the "else" statement is displayed as desired but the value still ends up in my array (myArray).

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

T Bart wrote:...If the user enters a value of 240 or greater, the message in the "else" statement is displayed as desired but the value still ends up in my array...

Welcome to the Ranch!

If the "else" block is executing, then I don't see how the "if" block could. Is it possible you have another line of code somewhere that's adding the value to the array? What happens if you comment out the assignment (line 4) in the code you posted? Does the value still get added?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
T Bart
Greenhorn

Joined: Apr 17, 2011
Posts: 5

Thanks for the quick reply! I thought that, too, about the statement. I remarked out the assignment part of the code and you are on the right track.The value still does end up getting entered into the array. I looked at every reference to myArray in my code and I can't see where the values would be getting added. Can you see anything? I'm posting the complete code.

Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
Hi, I think your code in lines 237 through 253 may be suspect. See if something there helps you get it in the right direction


Jason K. (AC)
T Bart
Greenhorn

Joined: Apr 17, 2011
Posts: 5

That's the section I originally posted. I remarked out the part that does the assignment to the array but the values still get added to the array. Is there something outside of that section that would be adding the values to the array as well? Weird!
T Bart
Greenhorn

Joined: Apr 17, 2011
Posts: 5

I found it. Lines 232 and 233 were outside the if-else statement and were adding to the total values without any evaluation. I moved those two lines of code to their respective places within each if-else statement and now everything is working properly. Thanks for your assistance!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

T Bart wrote:I found it. Lines 232 and 233 were outside the if-else statement and were adding to the total values without any evaluation. I moved those two lines of code to their respective places within each if-else statement and now everything is working properly. Thanks for your assistance!

Ah... So it was adding to the cumulative value, but not to the array, right?
T Bart
Greenhorn

Joined: Apr 17, 2011
Posts: 5

Yes, that is actually what was happening. When I would click the Run Report button, it would display the cumulative total which would include those invalid values.
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

Here is your modified code




The portion which has been modified is indicated as //modified
 
 
subject: If-Else help please