Hi I would like to know if something like subscripts can be used when naming object references. Subscripts might not be the best word to use, but it's hard to explain what I mean so I will give an example:
I would like to be able to do something like this:
The correct format would be (assuming x is declared somewhere else:
But even that isn't the normal idiom...you'd normally write it like this:
The problem with what you are trying to do is that your buttonx would only exist inside your for loop, due to scoping rules. You would have to declare the variables outside of the for loop.
Or, as Joanne says, use an array or other collection.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Feb 28, 2012
Thank you. I will try to use arrays to do what I had in mind.
Also thanks for informing me that my buttons will only exist in my loop. I also had a similar problem where my JFrame etc only exists inside my GUI building method, and I've just realised... It's my first time programming.
Don't you think it's a little early to begin with Swing even if you're obviously not familiar with basic Java constructs such as for loop? I would recommend you go one step at a time.
The quieter you are, the more you are able to hear.
Joined: Feb 28, 2012
Kemal Sokolovic wrote:Don't you think it's a little early to begin with Swing even if you're obviously not familiar with basic Java constructs such as for loop? I would recommend you go one step at a time.
I wouldn't say I am unfamiliar with a for loop. Perhaps with the syntax yes. The fact I forgot that the Buttons would only exist inside the for loop comes down to inexperience with Java. I have read Head First Java and so I am familiar with java to an extent but really need to actually write some code to improve.
When I said it was my first time programming that wasn't entirely true. I have used (not much though) MuPad on MatLab as part of my university course. The for loops there work slightly differently you see.
Besides I am only trying to make a simple noughts and crosses programme. I've made one that only works at the command line and other programmes that find primes and Fibonacci numbers etc. I don't think this is such a big step.
My intention was to give you an advice not to rush to Swing before you are sure you are well familiar with basic of Java (that includes syntax mentioned above). If you really want to learn to develop software with Java or any other programming language, that is the only correct path you should follow. If you are familiar with programming logic (from Matlab or any other) that's just a plus and will make the learning path less steep.
For a very fresh programmer, I would consider a problem like the one you had regarding the buttons being scoped inside the loop to be much more serious than remembering the syntax of a for loop. Scope is a very important concept in Java, and experienced programmers understand scope without really thinking about it. It's almost like second nature to experienced programmers. It's one thing to understand scope, it's another thing to grok it.
Beginner Programmers aren't expected to grok it like experienced programmers, but you are expected to have a very good understanding of it. I would heed Kemal's advice and not advance to Swing unless you have a good understanding of scope. Otherwise, you might hurt yourself