Results 1 to 9 of 9
Thread: A little newbie syntax problem
- 09-16-2012, 12:39 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
A little newbie syntax problem
Now I have a few variables named var1, var2, var3 etc. Now i wonder how can i get to them in an easy manner.
example of what i mean
So how do i write it to essentially getJava Code:for(int x=1;x<10;x++) { if(var(x).isEnabled()==true) }
if(var1.isEnabled()==true)
if(var2.isEnabled()==true) etc.
but in a way that it acctually works. Or am i forced to use tables?
Thanks
-
Re: A little newbie syntax problem
The == true part is unnecessary and clutters up your syntax.
Cleaner is to change this:
to this:Java Code:if (x == true) { // ... }
Java Code:if (x) { // ... }The statement above confuses me since you don't give us any information as to how or why the code isn't working. Please remember that we can't comment on code not seen.So how do i write it to essentially get
if(var1.isEnabled()==true)
if(var2.isEnabled()==true) etc.
but in a way that it acctually works. Or am i forced to use tables?
- 09-16-2012, 01:37 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: A little newbie syntax problem
Hmm i tried to be as clear as possible hmm here i go again variables are javax.swing.JCheckBox but that's irrelevant because what i wanted to know what is the proper way of writing
a loop that gets me if(var1.isEnabled()==true) {do something},if(var2.isEnabled()==true) {do something}, if(var3.isEnabled()==true) {do something} etc
So
And make it workJava Code:for(int x=1;x<10;x++) { if(var/*what do i write here to get 1, 2 ,3 etc*/.isEnabled()==true) { smth} }
- 09-16-2012, 01:49 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: A little newbie syntax problem
Use an array (of JCheckBoxes) and you can iterate over the array and manipulate each JCheckBox in the array.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-16-2012, 01:55 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: A little newbie syntax problem
Yeah thanks but i still wonder if there's a syntax that makes it possible to do this the way i explained (im really curious cause i tried for like 0,5h )
- 09-16-2012, 02:09 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: A little newbie syntax problem
Don't you have a textbook that talks about arrays? Something like this will do:
kind regards,Java Code:// declare and populate an array with JCheckBoxes JCheckBox[] var= { new JCheckBox(), new JCheckBox(), new JCheckBox() }; // set them all selected for (x= 0; x < var.length; x++) var[x].setSelected(true);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Re: A little newbie syntax problem
Ah yes, now I see what you're getting at. Yep, arrays or collections such as ArrayList<JCheckBox> are the way to go. The pseudo-code that you're trying to work just doesn't work for Java because variable names don't really exist (for the most part) in compiled code. It's all about references and getting a handle on them.
- 09-16-2012, 02:19 PM #8
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: A little newbie syntax problem
Thanks again but that's not what I wanted to know (im successfully done(thanks)) but what I want to know is it is possible to do so the way i described ;)
Ahh just read Fuburable's answer thanks guys
-
Re: A little newbie syntax problem
It is "technically" possible to do with class fields using reflection, not local variables, but this would be a terrible and ugly kludge and is not recommended. Again, use an array or a List.
Similar Threads
-
[SOLVED] problem with inheritance syntax
By Ollie999 in forum New To JavaReplies: 7Last Post: 07-18-2012, 12:04 PM -
Newbie Nested Loop Problem
By xcaldk74 in forum New To JavaReplies: 13Last Post: 03-06-2012, 05:03 PM -
Java Newbie - ArrayList Problem
By toxicrainpx in forum New To JavaReplies: 14Last Post: 06-05-2011, 06:14 PM -
Newbie problem
By hyookai in forum New To JavaReplies: 3Last Post: 03-07-2011, 02:30 PM -
[newbie] bracketing problem
By jon80 in forum New To JavaReplies: 15Last Post: 05-30-2009, 03:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks