Results 1 to 9 of 9
  1. #1
    Atkonis is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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
    Java Code:
    for(int x=1;x<10;x++)
            {
                
                if(var(x).isEnabled()==true)
               
            }
    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?
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: A little newbie syntax problem

    Quote Originally Posted by Atkonis View Post
    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
    Java Code:
    for(int x=1;x<10;x++)
            {
                
                if(var(x).isEnabled()==true)
               
            }
    The == true part is unnecessary and clutters up your syntax.
    Cleaner is to change this:

    Java Code:
    if (x == true) {
       // ...
    }
    to this:
    Java Code:
    if (x) {
       // ...
    }
    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?
    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.

  3. #3
    Atkonis is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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
    Java Code:
    for(int x=1;x<10;x++)
            {
                 
                if(var/*what do i write here to get 1, 2 ,3 etc*/.isEnabled()==true)
                { smth}
            }
    And make it work

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Atkonis is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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 )

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: A little newbie syntax problem

    Quote Originally Posted by Atkonis View Post
    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 )
    Don't you have a textbook that talks about arrays? Something like this will do:

    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);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  8. #8
    Atkonis is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. [SOLVED] problem with inheritance syntax
    By Ollie999 in forum New To Java
    Replies: 7
    Last Post: 07-18-2012, 12:04 PM
  2. Newbie Nested Loop Problem
    By xcaldk74 in forum New To Java
    Replies: 13
    Last Post: 03-06-2012, 05:03 PM
  3. Java Newbie - ArrayList Problem
    By toxicrainpx in forum New To Java
    Replies: 14
    Last Post: 06-05-2011, 06:14 PM
  4. Newbie problem
    By hyookai in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 02:30 PM
  5. [newbie] bracketing problem
    By jon80 in forum New To Java
    Replies: 15
    Last Post: 05-30-2009, 03:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •