File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Gui is running fine, don't know exactly how to code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Gui is running fine, don Watch "Gui is running fine, don New topic
Author

Gui is running fine, don't know exactly how to code

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

This is SUPER confusing. So I have a GUI. When you push Enter, two JLabels pop up saying First Editable Number: Yourfirstenternumberhere, Second Editable Number: Yoursecondenternumberhere. Please run it. Now, When you click on subTract, it subtracts from the numbers you typed in, no problem. When you click on it again, it starts from 0, subtracting 1 every time. If you enter numbers and click enter again, it will subtract 1 from the number the first time, then continue with the previous -X. Please help me with this. I didn't code the last part right, but I don't know HOW to code it so please help me with this. Please run it for yourself, change the numbers to whatever you want, click subtract. Please do this several times (2-5 times). Then go back to the input field and put in different numbers. Please click subtract several times (2-5). Now you see my problem. If anyone knows how to "fix" this please, please tell me. There are no compiler or running errors (just the normal SYSTEM errors if you click subtract before you enter a number, which ISN'T A PROBLEM). The problem has something to do with beck and heck, because when I set them = to another number in the Risk class, it started counting down the second time from that. I don't think heck and beck are being "updated".
Risk.java:

RiskApp.java:

Thanks everyone,
cc11rocks


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Don't make us copy, compile, and run your program 2-5 times, multiple times to discover on our own what it's doing wrong, especially when you don't even explain or document in your code what it's supposed to do. Explain all that. Explain what you would like it to do, what it is doing, where you think the problem might be, and list any errors you're getting. It's also helpful to show inputs with expected outputs and actual outputs, explaining how the actual outputs are wrong.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

john price wrote:This is SUPER confusing.

Yes, it is. Perhaps you should take the time to make a simple version of your program that either demonstrates your problem (or works) that has no GUI. This would be something much smaller and easier for us to help you with.

john price wrote:So I have a GUI. When you push Enter, two JLabels pop up saying First Editable Number: Yourfirstenternumberhere, Second Editable Number: Yoursecondenternumberhere.

I can't see the label for the second editable number. But I doubt that this is the problem that you want looked at. Again - if we had an example that was easy to work with, you might get help from lots of people.

john price wrote:Please run it.

As Greg mentions, this is probably the worst way to ask for help. You really want to make it as easy as possible for people to help you. Anything that makes it hard for them to help you is likely to result in them moving on to another problem.

john price wrote:Now, When you click on subTract, it subtracts from the numbers you typed in, no problem. When you click on it again, it starts from 0, subtracting 1 every time.

Although you never say so, I assume that the fact that it restarts at zero is the problem you want help with.

john price wrote:Now you see my problem.

I see many problems that you might want help with. One of which you have identified as not a problem. Again - you really want to make this easy for anyone who might be able to help you.

john price wrote:The problem has something to do with beck and heck, because when I set them = to another number in the Risk class, it started counting down the second time from that. I don't think heck and beck are being "updated".

You should consider having reasonable variable names as well, so that the code is easy for others to understand.

To explain what I mean about making the code easy to read, consider Risk.java:
  • line 11 - setbad and setgood variables are being used for what would normally be boolean gates, but they are integers.
  • line 15 - variables are not used
  • line 16 & 17 - identical variables that are only used in one place (so do not need to exist)
  • line 22 & 23 - variables not used
  • line 10 & 19 & 20 - variable "smart" never used after initial setting
  • line 10 & 26 & 27 - variable "dumb" never used after initial setting
  • line 30 - variable not used
  • line 8 - variables not used after being defined in lines 30 - 35
  • line 47 & 48 - unneeded constructor

  • I could go on (and on, and on). But hopefully you can get an idea of the problem anyone looking at your code faces - in order to find your real problem, I have to wade through lots of other problems and determine whether they are serious issues or just bad coding.

    Anyway, the problem you have is one of scope. Take a look at lines 78 & 79 - what is the scope of those two variables? What scope do you need?


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    Here is the program and comments through pictures. Space between pics is provided by my text (actual typed text like now, not in the picture). There are 10 Pictures in total. Hopefully this answers any questions you have.
    Pic 1(start):

    Pic 2:

    Pic 3:

    Pic 4:

    Pic 5:

    Pic 6:

    Pic 7:

    Pic 8:

    Pic 9:

    Pic 10:

    Please help me fix my code.
    Thanks,
    cc11rocks
    PS: There are no errors whatsoever!
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Also, as a matter of etiquette, you should mention when you post the same problem in another forum and another forum. It is not necessarily a problem doing this, but it would be nice to know if you already have an answer somewhere else before we spend time trying to help you here.

    Likewise, now that you have the solution in this forum, it is considered polite to go back to the other forums and state that you have the solution here.
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    Andrew Monkhouse wrote:Also, as a matter of etiquette, you should mention when you post the same problem in another forum and another forum. It is not necessarily a problem doing this, but it would be nice to know if you already have an answer somewhere else before we spend time trying to help you here.

    Likewise, now that you have the solution in this forum, it is considered polite to go back to the other forums and state that you have the solution here.

    I will keep that in mind for next time thanks. I have posted this in 5 different forums now because I have not received a sufficient answer to my question. I still do not feel as if my question has been answered. I have posted my complete code and taken screenshots with comments demonstrating and pointing out the exact problem. I don't know how much more specific I can get. If you mods want to close this, I will respect that and will not post again, but I feel this should NOT be closed, because it has not been sufficiently answered.
    Please help me!
    Much Appreciation,
    cc11rocks
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    john price wrote:I have posted this in 5 different forums now because I have not received a sufficient answer to my question. I still do not feel as if my question has been answered. ... If you mods want to close this, I will respect that and will not post again, but I feel this should NOT be closed, because it has not been sufficiently answered.

    No need to close this - we can keep discussing it until you see the solution. It is not a problem.

    Going back to the last sentence of my 1st post:
    I wrote:Anyway, the problem you have is one of scope. Take a look at lines 78 & 79 - what is the scope of those two variables? What scope do you need?

    Take a look at lines 78 & 79. You are declaring two variables there. When you declare variables, what is the scope of them? That is - what other code can see those variables you just declared? Once the method completes, what happens to those variables you just created?
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    Which ones out of these?

    Just making a confirmation here.
    cc11rocks
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Sorry, I know that my hinting around the solution without explicitly stating it must be extremely frustrating for you. But I have a reason - if I can get you to find the problem for yourself, then you will learn far better, and it will be far less likely for you to have these problems in the future.

    When I am talking about line numbers, the only ones I can use are the ones that you posted in your first post. You have presumably changed your local copy of the code. However I can't see that, so I can't use those numbers. So lines 78 & 79 (from what I know of) are:

    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    To my knowledge, these statements take the strings nos and yos and convert them to an int. The ints are heck and beck, respectively. And yes, it is VERY frustrating but I know you are trying to help me.
    Sincerely,
    cc11rocks
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    OK, lets jump out of your specific problem, and consider a more generic problem. Why wont the following code compile?

    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    Two issues:
    You never initiated innerscope and it will always be true so it will ALWAYS = 1.
    cc11rocks
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Hmmm, that doesn't answer why it won't compile. Let's try it slightly differently:

    What will be printed at line 9, and what will be printed at line 12?

    The two answers are different, and this is the core to your problem.
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    Andrew Monkhouse wrote:Hmmm, that doesn't answer why it won't compile. Let's try it slightly differently:

    What will be printed at line 9, and what will be printed at line 12?

    The two answers are different, and this is the core to your problem.

    Should I compile, see, and check or just think about it?
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    They would either both be 3 or one is 1 and two is 3.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    john price wrote:Should I compile, see, and check or just think about it?

    Compiling it would be good to confirm your suspicions.

    Your initial thought - that both would be 3 - is incorrect. Your later thought - that the first printed statement is 1 and the second is 3 - is correct. Any ideas why?
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    It only temporarily changes the value for some reason?
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Actually no, it doesn't. There are two separate variables involved:

    There is a variable named changeableScope at line 2, which has global scope. It is visible throughout the entire class.

    There is a totally separate variable named changeableScope at line 8, which is only visible within the scope of the closest braces (open brace on line 8, close brace on line 10). Once that variable goes out of scope, the remainder of the class cannot see it. So line 12 is referring back to the global variable.

    This is referred to as shadowing, and is possibly one of the most common problems that people make.

    Two things you could try -
  • change the name of the variable in line 8 to something different (but don't change any other part of the program) and see what results you get
  • keep the variable in line 8 as "changeableScope" but stop it from being a new definition - remove the "int" from the start of the line. See what results you get.


  • Does this make sense? Does it help you spot a problem in your code?
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    I removed the ints...This is how the code looks like now (lines 78-79):

    Thank you so much. I will look into shadowing more. I have heard of it and probably read OVER (didn't pay much attention) to it. Thank you so much! I'm new to Java, as you can probably tell. I can't believe that was my mistake. So it's basically saying "Since you've already defined heck and beck, you want something new for this only. We'll give it to you and then go back to the origanal" Right?
    With gratitude,
    cc11rocks
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    john price wrote:I can't believe that was my mistake.

    I thought you'd feel that way when you realized what was happening. It is great to see you got it. Congratulations!

    john price wrote:So it's basically saying "Since you've already defined heck and beck, you want something new for this only. We'll give it to you and then go back to the origanal" Right?

    Exactly.
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    New code using your suggestions. Apparently, I don't know how to use boolean values. Could you please help?
    RiskApp.java stayed the same so I'm not gonna bother posting it.
    Risk.java:

    7 Errors:

    I'm going to bed so I won't be able to talk to you until tomorrow. Thanks for all your help. I REALLY appreciate it!
    Thank you,
    cc11rocks
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    You have defined setbad and setgood in lines 17 & 18. To determine the scope of these variables, you need to look for the closest surrounding braces. There closest previous opening brace is at line 16, and the closing brace that matches that opening brace is at line 39. Therefore once you are outside of those two braces, no other part of the code can access those variables.

    The first error indicates that when it tries to use the variable setbad at line 46, it cannot find it. This is correct - as per my previous paragraph, once you go beyond line 39, setbad is out of scope and can no longer be accessed.

    To fix this, just move setbad and setgood up to class level variables.
    john price
    Ranch Hand

    Joined: Feb 24, 2011
    Posts: 495

    Thank you very much.
    Risk.java (working 100%, no errors):
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Gui is running fine, don't know exactly how to code
     
    Similar Threads
    General question about ActionListeners accessing other GUI elements
    Serious issue: NumberFormat String Parse int problem
    Code cleanup
    Clicking works, but pushing "enter" return errors...how to fix? (on a button named "Enter")
    i need help with a simple add, subtract, cal. applet