my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes if statement problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "if statement problem" Watch "if statement problem" New topic
Author

if statement problem

Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
I am new to Java Scripting and I am trying to write some code or if statement for the following. I have 2 JButtons (one labeled btnAddTwo and one lableled btnSubOne) and one text field.I am trying to get the btnAddTwo to add 2 when I click it and the btnSubOne to subtract one from the total in the text field. I built my program in netbean as a gui frame. Everthing works except for my counting. Any help I can get would be great - I am also new to the Bis Moose.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Don Hays wrote:I am new to Java Scripting and I am trying to write some code or if statement for the following.

Welcome to the Ranch! You use of the term "scripting" kind of threw me off, since it suggests JavaScript which is an entirely different language; but no matter.

I have 2 JButtons (one labeled btnAddTwo and one lableled btnSubOne) and one text field.I am trying to get the btnAddTwo to add 2 when I click it and the btnSubOne to subtract one from the total in the text field. I built my program in netbean as a gui frame. Everthing works except for my counting.

Let's see your actionPerformed method. Also, if you are getting any error messages, please let us see the actual messages, the line of code causing it, and any other pertinent bits of information.

Again, welcome!
Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Pete
Here is my GuiFrame info - Thanks for the help

Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Pete
Here is my AppLauncher info.

Don

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I added code tags to your two posts above in order to help your posted code retain its formatting and thereby be readable.

To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:



Regarding your code, one problem that I see is that some of your if statements have errant semicolons that can be messing up your logic. For instance if you have this statement:


the semicolon after if (foo) will short-circuit the if block so that what really happens is this:




The solution is to have no semicolon there:

Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Pete
Thanks for the information I will work with it this weekend and see if I can get it to work. I'm not sure what you meant about the tags you added to my code - I could not see any changes to the code I sent you. This Java is a challenge and I hope I am up to the challenge. the project seems simply to click one button and add 2 and then click the other button and subtract one from the same text field. Of course it is easier said then done. Thank you for all you help.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Pete was referring to http://faq.javaranch.com/java/UseCodeTags


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Maneesh & Pete
Thanks for the replies - sorry if I caused any confusion or problems - I think when you are frustrated with coding the brain cells are on over load. I think the Java Ranch is great and I will take the time to read the rule of the road when using the forums. In the future I will keep the posts short and to the point and use the [code] tags. Again thanks for your help - I hope I am in a position to help someone else down the road when I feel I can. Don
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Don Hays wrote:Thanks for the replies - sorry if I caused any confusion or problems - I think when you are frustrated with coding the brain cells are on over load.

There's certainly no need to apologize. But please do tell us if you've solved your current problem or not, and if not, then let's see your most recent code attempt and a description of the problem or posting of the error messages.


I think the Java Ranch is great and I will take the time to read the rule of the road when using the forums. In the future I will keep the posts short and to the point and use the [code] tags. Again thanks for your help - I hope I am in a position to help someone else down the road when I feel I can. Don

I'm sure that you will be, and probably sooner than you think!
Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Pete - I am still struggling with this code. I hope I paste this right. When I run my program the buttons and text field show up in my gui frame and when I push either button the frame exits. If I change the boolean to true the same thing happens.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Regarding this, a couple of things:



When you call getSource on the ActionEvent parameter, here called ae, it returns the component that triggered the action, here a JButton, so this makes no sense:


since source is a JButton and will never == a String such as "btnAddTwo"

If your button variables are still declared like so:


Then check if the source variable and btnCount variable refer to the same object via:


There's another way to do this check by comparing Strings using the String returned from the ActionEvent's getActionCommand method, but to keep things simple, I'll put this on the back burner.

Also, what are you trying to achieve with this statement?

and the similar statement further down in the method. They seem to be wasted statements having no effect on program logic.

Also, are you trying to add two to a number already held by one of the JTextFields? If so, you'll need to get that number first by extracting the String held by the JTextField by calling getText() on the JTextField, then parsing or translating the String into an int via the Integer.parseInt(String s) method, rather than using some other variable "x".

The steps as I see them in pseudocode are thus:

Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Pete
Obviously I am a true green horn on java. I am off to a funeral for a good friend and I will spend tomorrow reading a few chapters in my book so I can get a better handle on the logic I need to use when using strings. I have had html, xml and xhtml classes and now I am trying to tackel java. I will solve this issue and send you my results - I appreciate you patients and help. As far as the boolean code I misread the last post Maneesh - I thought they were telling me to us a boolean to solve the problem. My mistake! Thanks again for all your help. Don
Don Hays
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Pete
I finally got it to work - a little reading and experimenting is good for the mind. Thanks for the help! Following is the code I got to work. The btnCount adds two to the text field and exits at 12 and the btnCount2 subtracts one from the same text field and exits at 0.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if statement problem