This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

named after a count  RSS feed

 
peter huff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i have some code which is shown below. Basically i want to have a for loop that will output a 9x9 grid of textfields, all of which will be named sq1 through to sq81 depending on their position, however I cant seem to join "sq" with "count" to get the number. any help? thanks

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand this: " I cant seem to join "sq" with "count" to get the number".

Meanwhile, in your code, you seem to have forgotten to put your loop body in braces. Was this your problem?
 
peter huff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah im sorry didnt explain very well. the loop is meant to produce a 9x9grid of textboxes. i have managed to do this with a loop, but none of the textboxes had a name. so i am now trying to name them and i thought i could do this using the value of "count" as a postfix to a name. so the first textfield would be sq1 the next sq2, sq3 sq4.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean like this:

The parens are needed, otherwise you append 2 numbers instead of one: their sum.
 
peter huff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess but i cant implement that into the code above, im used to working in c and i know how to do it in that just not java
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by peter huff:
i guess but i cant implement that into the code above, im used to working in c and i know how to do it in that just not java

?? You lost me again. Couldn't you write:Perhaps what would be more useful is if you backed up and wrote what your goal is, what you are trying to do and what you are having a problem with. I'm willing to bet dollars to doughnuts that your real problem has not been expressed.
 
peter huff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried using that but it just brings up the following error

Board.java:10: cannot find symbol
symbol : variable sq
location: class Board
sq[count]=new TextField(2);
^
Board.java:11: cannot find symbol
symbol : variable sq
location: class Board
sq[count].setName("sq" + (count+1));
^
Board.java:12: cannot find symbol
symbol : variable sq
location: class Board
add(sq[count]);
^
3 errors


basically im trying to produce a 9x9 grid of textfields each with its own unique name. i could do it manually by creating each of the 81 textfields individually, however, i should be able to do this as a loop which is where the difficulty arises.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an unrelated problem -- you never declared the "sq" variable.

Henry
 
peter huff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, well i declared it as int, but i still got errors at compile. im new to java. how would you do it henry?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by peter huff:
ok, well i declared it as int, but i still got errors at compile. im new to java. how would you do it henry?


You may be new to java, but even in C you have to choose a variable's type carefully, right? For example, to choose the appropriate struct. You already have an int counter in "count". Now what would be a more appropriate type for sq? How is sq used? It's clearly not being used as an int, since you write "sq[count] = new TextField(2);"
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by peter huff:
i guess but i cant implement that into the code above, im used to working in c and i know how to do it in that just not java


So how WOULD you do this in C? Neither C nor Java has any facility to create variable names on the fly like you say you want to do. However, you can use arrays to do something similar. In fact, the syntax you are using implies that you want to use an array. But an array of what? I think if you can figure out the answer to this question, you will start making some progress here.

Layne
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arg! The robots are attacking!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrectson:
Arg! The robots are attacking!


it's being addressed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!