• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextboxes in an array  RSS feed

 
Dee Heat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this not work? I get an ']' expected at the y in the fourth line of code and an illegal start of an expression at the actual ] in the same line


int y = 0;
JTextField response[] = new JTextField[10];
while(y < 10) {
JTextField response[y] = new JTextField(7);
add(response[y]);
response[y].addItemListener(this);
++y; }
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This line, right? The compiler sees a type name ("JTextField") at the beginning and so it decides you want to declare a variable. Then it sees "response" and decides that's the name of the variable you want to declare. Then it sees "[" and decides you're declaring an array variable... and then it sees the "y" and now it doesn't make sense because that's not how you declare an array variable.

But you don't want to declare a variable there. You've already declared "response" two lines earlier and you don't need to do it again. Just write an ordinary assignment statement to assign something to an array element.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!