This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to char

 
Imtiaz Nizami
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code which works fine:

cellPanel.add( new JButton(new CharIcon(new Character('1'))) );
cellPanel.add( new JButton(new CharIcon(new Character('2'))) );
cellPanel.add( new JButton(new CharIcon(new Character('3'))) );
cellPanel.add( new JButton(new CharIcon(new Character('4'))) );
cellPanel.add( new JButton(new CharIcon(new Character('5'))) );
cellPanel.add( new JButton(new CharIcon(new Character('6'))) );
cellPanel.add( new JButton(new CharIcon(new Character('7'))) );
cellPanel.add( new JButton(new CharIcon(new Character('8'))) );
cellPanel.add( new JButton(new CharIcon(new Character('9'))) );

I need to change it to something like:
for (int i=0; i<9; i++)
{
cellPanel.add( new JButton(new CharIcon(new Character(i))) );
}

except that there is no Character constructor that takes in an integer. Is there a quick solution? I want to avoid Strings and Character Arrays. Thanks.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the for loop, the i isn't required by Java to be an int. (It could be a different data type, like the one you actually want it to be.)

Or...

When you create the new Character, you could cast the int i to a different data type.
[ June 10, 2007: Message edited by: Dirk Schreckmann ]
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can use this code

 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
In the for loop, the i isn't required by Java to be an int.


Correct. This compiles:

 
Imtiaz Nizami
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic