• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to access my variable out side actionListener  RSS feed

 
Christopher thompson
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to acces my variable "cost" so when i press a button it prints the cost(for now) but the int value just prints 0


 
fred rosenberger
lowercase baba
Bartender
Posts: 12442
42
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense, but this is a terrible design. It will not scale at ALL if you want to add 20 more locations, 10 more accomodations, and 5 more nights.

having said that...why do you keep declaring cost over and over again inside each 'if' (i.e. lines 67, 70, 76, and others)? each time you do that, you create a new cost variable local to the if-block, which then immediately falls out of scope and is thus gone. Of course, you don't do that on lines 44 and 40, i believe that if you choose the options that gave you that execution path you would see cost being set to something.
 
Christopher thompson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:No offense, but this is a terrible design. It will not scale at ALL if you want to add 20 more locations, 10 more accomodations, and 5 more nights.

having said that...why do you keep declaring cost over and over again inside each 'if' (i.e. lines 67, 70, 76, and others)? each time you do that, you create a new cost variable local to the if-block, which then immediately falls out of scope and is thus gone. Of course, you don't do that on lines 44 and 40, i believe that if you choose the options that gave you that execution path you would see cost being set to something.


yes i know but I'll cross that bridge when i get there lol, I'm just practicing at the moment but i have tried those options and still get 0.
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher thompson wrote:
fred rosenberger wrote:No offense, but this is a terrible design. It will not scale at ALL if you want to add 20 more locations, 10 more accomodations, and 5 more nights.

having said that...why do you keep declaring cost over and over again inside each 'if' (i.e. lines 67, 70, 76, and others)? each time you do that, you create a new cost variable local to the if-block, which then immediately falls out of scope and is thus gone. Of course, you don't do that on lines 44 and 40, i believe that if you choose the options that gave you that execution path you would see cost being set to something.


yes i know but I'll cross that bridge when i get there lol, I'm just practicing at the moment but i have tried those options and still get 0.


You really should read all of fred's response, he basically answered your question.

Henry
 
Christopher thompson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Christopher thompson wrote:
fred rosenberger wrote:No offense, but this is a terrible design. It will not scale at ALL if you want to add 20 more locations, 10 more accomodations, and 5 more nights.

having said that...why do you keep declaring cost over and over again inside each 'if' (i.e. lines 67, 70, 76, and others)? each time you do that, you create a new cost variable local to the if-block, which then immediately falls out of scope and is thus gone. Of course, you don't do that on lines 44 and 40, i believe that if you choose the options that gave you that execution path you would see cost being set to something.


yes i know but I'll cross that bridge when i get there lol, I'm just practicing at the moment but i have tried those options and still get 0.


You really should read all of fred's response, he basically answered your question.

Henry


yes i did, i was talking about lines 44 and 40. in fact i have corrected it all and still get 0;
 
Tony Docherty
Bartender
Posts: 3205
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher thompson wrote:i have tried those options and still get 0.

If you have done as Fred suggested and removed all the local declarations of cost then cost must be being declared somewhere else with wider scope in your code or it wouldn't compile so if cost = 0 then none of your if statements are evaluating to true or you are setting cost back to 0 somewhere else in your code. I suggest you add some print statements to output the actual value of comboDest1.getSelectedItem(), comboAccom1.getSelectedItem() and comboNights.getSelectedItem() to ensure they are returning the values you are expecting (remember to surround the values with a visible char eg ' to so you can see any spurious leading or trailing whitespace) and also add a print statement wherever to are setting the value of cost to 0 so you can see the execution order.
 
Christopher thompson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried that too the prints statement works when the if statements are true, I made some amendments now the print(cost) statement is coming up as an error >.< I'm not 100% as if i declared it in the right place I will add full code to the op but its fairly long
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid I went back to your post to try to improve the formatting; the long lines make it difficult to read. That is the worst feature, but you also seem to have inconsistent indentation, mixing tabs and spaces, and you have too many bnlank lines. You also appear to have /**documentation comments*/ for a method but followed by a right brace }. That makes it very difficult to work out where methods and classes begin and end; the style of code suggests you are using Eclipse, so I am surprised it has not improved the formatting. You also have things like + JoptionPane.PLAIN_MESSAGE; you are supposed to pass that sort of value as an argument, not use it in arithmetic or String catenation. I am a bit surprised you got that code to compile.

Fred Tony and Henry are, I am afraid, going to appear bright lights of joy and happiness compared to me. I think you will find it impossible to correct that code at all and I think you are going to have to start from scratch. Sorry to bring bad news. I do not think you will ever be able to cross that bridge when you come to it. Sorry.

I think your only hope is to create objects representing types of holiday. That is awkward: all this stuff about 7 days or 14 days looks more like something out of a database than programming.
I shall write more in a few minutes.
 
Christopher thompson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL. No problem, sorry i guess i have bad practices I'm new but i take everything you guys are saying onboard
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should consider different types of class to represent different kinds of holiday. I am not sure what sort you should use. You might find an enum like this works:-You can read about enums in the Java Tutorials, and look at this example which Winston Gutkowski posted earlier this week.

You want to have an app which runs from the command line without a single Swing component in sight. You want to be able to enter Paris Youth Hostel 7 and have it tell you that costs £229 (or whatever). You don't want any real data inany of your Swing classes. They are there to create a display, not for doing logic. The logic should be in other classes, as I said, without a Swing component in sight.
When you have got that working, then you can consider putting a GUI on top of it.
 
Christopher thompson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Henry, fred, Tony and Campbell. I've looked at that site and this youtube vid starting to get a better understanding
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!