Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

If-Else help please

 
Greenhorn
Posts: 5
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
T Bart
Greenhorn
Posts: 5
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
T Bart
Greenhorn
Posts: 5
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is your modified code




The portion which has been modified is indicated as //modified
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic