• Post Reply Bookmark Topic Watch Topic
  • New Topic

Buttons not working properly

 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I want my program to listen to jl1 and read a random activity from b2 eachtime jl1 is pressed. For some reason each time I press jl1 nothing happens until the second time I press it then it always gives the same random response. I need some advice please.






























 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by fixing the formatting of your code. You do not use consistent indentation of the code so it is difficult to read blocks of code.

The common formats are:



That is you separate each if statement by a blank link and you indent the code in the statement a consistent amount.

The problem with your code is that you are missing a "}" on one of your if statements so only one if statement is ever executed.

You probably can't see this because of your inconsistent formatting.

Formatting makes the code more readable an less error prone.

Also, did you add any debug statements to your code. For example did you ever display the value of the random variable or the value of the String? You can't expect to solve problems just by staring at the code.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I can fiy the formatting of code to this style no problem and seperate each if statement by a blank line. Thanks for the find there was wondering why it semi worked but I didn#t get an error. Thanks for the tips it will sink in with practice. This is the first I have heard about debug statements however. I don´t know how to display the value of a random unless I println it. Is that what you mean?
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don´t know how to display the value of a random unless I println it. Is that what you mean?


Yes. That is what debugging is about.

Finding out why the value of a variable is not what you expect to be. Finding out why the code doesn't execute in the order you expect.

Displaying the value of variables or adding a println() statement in an if condition will confirm if the code executes as you expect.

You can't expect to post a question in a forum every time you have a little problem. You need to develop your own problem solving techniques.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on it. Just need different perspectives like yours to bring it together for me. Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!