String maniplulation
publicvoid fillGrid(int gridSize){
for(int i = 1; i <= gridSize; i++){
String squareName ="sq" + i;
Square squareName =new Square(i);
grid.addElement(squareName);
//System.out.println("Square: " + sq.getID());
System.out.println(squareName);
}
}
Hi all, you can tell from the code what i'm trying to do, i.e. make new Square objects with different names within a loop. But the error msg i get is that "the variable squareName is already defined". How can i get round this problem, so i can have different Square object names, for e.g. Square objects sq1, sq2, sq3 and so on?
[1002 byte] By [
djsinlesa] at [2007-11-26 16:27:49]

>String squareName = "sq" + i;
> Square squareName = new Square(i);
This doesn't work, two different variables in the same scope need to
have different names to identify them. Have a look at this example:LazyPerson me= new LazyPerson();
ADHDPerson me= new ADHDPerson();
me.jumpUpAndDown();
Which of 'me' is going to protest and which of me is going to jump all day?
kind regards,
Jos
> >String squareName = "sq" + i;
> > Square squareName = new Square(i);
>
> This doesn't work, two different variables in the
> same scope need to
> have different names to identify them. Have a look at
> this example:> LazyPerson me= new LazyPerson();
> ADHDPerson me= new ADHDPerson();
> me.jumpUpAndDown();
> Which of 'me' is going to protest and which of me is
> going to jump all day?
>
> kind regards,
>
> Jos
i know two different variables need to have different names to identify, but that's the problem, i need to find a way to make new Square objects with different names within a loop, so how do i do it?
> i know two different variables need to have different names to identify,
> but that's the problem, i need to find a way to make new Square
> objects with different names within a loop, so how do i do it?
I don't know what you want to accomplish, but if you change these two lines:String squareName = "sq" + i;
Square squareName = new Square(i);
to this:Square squareName = new Square("sq"+i);
... and add a new Square constructor like this:public Square(String name) { ... }
... at least you got rid of those duplicate names. You have to elaborate
a bit when the above doesn't make sense to you.
kind regards,
Jos
> String squareName = "sq" + i;
> Square squareName = new Square(i);
> to this:
> Square squareName = new Square("sq"+i);
> ... and add a new Square constructor like
> this:> public Square(String name) { ... }
> ... at least you got rid of those duplicate names.
> You have to elaborate
> a bit when the above doesn't make sense to you.
>
> kind regards,
>
> Jos
All i want is to make New square objects each time around the for loop. I want each new Square in each loop to have a different name, e.g. sq1, sq2 ...
> Square squareName = new Square("sq"+i);
> ... and add a new Square constructor like
> this:> public Square(String name) { ... }
This i can do but all the squares created in each loop will have the same name 'squareName', so how will i be able to distinguish between the 25 squares which i will create using the loop.
Does it really matter if the squares created are called the same, when they will have different atrribute data?
> All i want is to make New square objects each time around the for
> loop. I want each new Square in each loop to have a different name,
> e.g. sq1, sq2 ...
You can't invent new identifier names at runtime. What you can do is
build a Map that associates a string, like "sq1", "sq2" ... with a new
Square object.
> Does it really matter if the squares created are called the same, when
> they will have different atrribute data?
Of course it matters, otherwise we could call everything "Joe" or "foo"
and still be able distinguish one from the other.
Here's a little start w.r.t. that Map suggestion I gave you (see above):Map<String, Squares> squares= new HashMap<String, Squares>();
for (int i= 0; i < nofSquares; i++)
squares.put<"sq"+i, new Squares(i));
now you can find "sq"+i from the map like this:int i= 42;
Square thisSquare= squares.get("sq"+i);
kind regards,
Jos
Or he could look into arrays or Collections.
OK, but i still dont fully understand ur (map) code,
This is what i have:
public void fillGrid(int gridSize) {
for(int i = 1; i <= gridSize; i++) {
String squareName = "sq" + i;
Square squareName = new Square(i);
grid.addElement(squareName);
//System.out.println("Square: " + sq.getID());
System.out.println(squareName);
}
}
And this is what u gave me (map code):
> Map<String, Squares> squares= new HashMap<String,
> Squares>();
> for (int i= 0; i < nofSquares; i++)
>squares.put<"sq"+i, new Squares(i));
> you can find "sq"+i from the map like this:
> nt i= 42;
> Square thisSquare= squares.get("sq"+i);
Can u please alter my code to fit the map code, it's because i cant follow ur code, hence can't implement myself, the constructor for the Square class if as follows:
public Square(int id) {
this.id = id;
}
Not the Map-style Jos proposed, but this works as well (and is a bit easier IMHO):
Square[] arrayOfSquareObjects = new Square[10];
for(int i = 0; i < arrayOfSquareObjects.length; i++) {
arrayOfSquareObjects[i] = new Square(i);
}
You should probably read through these: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html http://java.sun.com/docs/books/tutorial/collections/index.html
> Not the Map-style Jos proposed, but this works as
> well (and is a bit easier IMHO):
> [code]Square[] arrayOfSquareObjects = new
> Square[10];
> for(int i = 0; i < arrayOfSquareObjects.length; i++)
> {
>arrayOfSquareObjects = new Square(i);
> /code]
Does that mean i dont need the Vector grid? just creat a Square array instead?
> Does that mean i dont need the Vector grid? just
> creat a Square array instead?
I don't know, it's your code.
But since grid is a Vector, why not just do this:
public void fillGrid(int gridSize) {
for(int i = 1; i <= gridSize; i++) {
grid.addElement(new Square(i));
}
}
?
@OP: I think you really should read the links supplied to you in reply #9.You don't know what you're doing (yet) and by just hacking your waytowards an answer you don't understand is not the way to go.no insult intended and,kind regards,Jos
why do i get this error msg in code line 4, ')' expected?
public static void makeMove(Square sq) {
if (sq.isFilled() == false) {
System.out.println("card placed on square " + sq.getID());
sq.Fill(Card c);
}
else
System.out.println("sorry, a card is already placed on square " + sq.getID());
}
Explain to me what it is your doing on this line:sq.Fill(Card c);
it's the fourth line in the following lines of code:
public static void makeMove(Square sq) {
if (sq.isFilled() == false) {
System.out.println("card placed on square " + sq.getID());
sq.Fill(Card c);
}
else
System.out.println("sorry, a card is already placed on square " + sq.getID());
}
> Explain to me what it is your doing on this
> line:sq.Fill(Card c);
public static void makeMove(Square sq) {
if (sq.isFilled() == false) {
System.out.println("card placed on square " + sq.getID());
sq.Fill(Card c);
}
else
System.out.println("sorry, a card is already placed on square " + sq.getID());
}
well i just want to send a Card object and record it in the Square objects Card attribute
this is the fill method from the Square Class:
public void Fill(Card card) {
this.filled = true;
this.card = card;
}
You're doing it wrong. Try this:sq.Fill(new Card());By the way, method names should start with lower-case letters.
> public static void makeMove(Square sq) {
> ...
>sq.Fill(Card c);
> ...
> }
>
> well i just want to send a Card object and record
> it in the Square objects Card attribute
Ok, but shouldn't you put an instance of a Card in it?
I presume you know how to instantiate an object?
> public void Fill(Card card) {
>this.filled = true;
>this.card = card;
>}
Java's naming convention says method names should start with a small letter, not a capital.