Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > increment letters

Reply
Thread Tools

increment letters

 
 
glennsnoise@hotmail.com
Guest
Posts: n/a
 
      07-31-2007
Hi. I'm designing a Wheel-of-Fortune kind of layout using JButtons
with ActionListeners to guess letters for words (kind of like Hangman,
but using JButtons). I've set up a for loop that gives me 26 JButtons,
but right now, all 26 buttons display "A". Can anyone tell me how to
add an increment function to this so that each button will display a
different letter of the alphabet?

Here is what I have so far:


import javax.swing.*;
import java.awt.*;

public class GhostGUITwo extends JFrame {
public static void main(String args[]) {
new GhostGUITwo();
}
//FRAME
public GhostGUITwo() {
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.setTitle("Ghost");
this.setLocationRelativeTo(null);
GhostGUITwoPanel panel = new GhostGUITwoPanel();
this.getContentPane().add(panel);
this.setVisible(true);
}
//PANEL
public class GhostGUITwoPanel extends JPanel {
public GhostGUITwoPanel() {

for (int i = 0; i < 26; i++)
this.add(new JButton("A"));
}
}
//add JLabel and ActionListener
//JButton ClickMe adds corresponding letter to JLabel
}

 
Reply With Quote
 
 
 
 
Twisted
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 4:03 pm, glennsno...@hotmail.com wrote:
> this.add(new JButton("A"));


Try

add(new JButton(""+'A'+i));

This works in C; less sure about Java. There's also:

add(new JButton(LETTERS[i]));

with elsewhere in the class the field

private static final String[] LETTERS = {"A","B","C",...,"Z"};

with the ... filled in in the obvious manner.

The array method is more amenable to any subsequent i18n conversions.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-31-2007
On Tue, 31 Jul 2007 13:03:56 -0700, wrote,
quoted or indirectly quoted someone who said :

> Can anyone tell me how to
>add an increment function to this so that each button will display a
>different letter of the alphabet?


just use a for loop:

for ( char letter= 'A'; letter <= 'Z'; letter++ )

In Unicode you can count on the letters being in order and contiguous.
see http://mindprod.com/jgloss/unicode.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-31-2007
On Tue, 31 Jul 2007 20:16:37 -0000, Twisted <>
wrote, quoted or indirectly quoted someone who said :

>
>add(new JButton(""+'A'+i));
>
>This works in C; less sure about Java. There's also:


That won't work. The problem is + promotes 'A' to an int. You then
effectively have ""+ 65 which will generate the string "65".

There are a number of related gotchas. See
http://mindprod.com/jgloss/gotchas.html#CONCATENATION
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      07-31-2007
On Tue, 31 Jul 2007 20:16:37 +0000, Twisted wrote:

> On Jul 31, 4:03 pm, glennsno...@hotmail.com wrote:
>> this.add(new JButton("A"));

>
> Try
>
> add(new JButton(""+'A'+i));
>
> This works in C; less sure about Java. There's also:


The correct form is add(new JButton(""+(char)('A'+i)));

What you wrote would (I think) give you "A0","A1", etc.
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 4:25 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 31 Jul 2007 20:16:37 -0000, Twisted <twisted...@gmail.com>
> wrote, quoted or indirectly quoted someone who said :
>
>
>
> >add(new JButton(""+'A'+i));

>
> >This works in C; less sure about Java. There's also:

>
> That won't work. The problem is + promotes 'A' to an int. You then
> effectively have ""+ 65 which will generate the string "65".


Yeah, I figured it might need a cast in there somewhere.

 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 4:22 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 31 Jul 2007 13:03:56 -0700, glennsno...@hotmail.com wrote,
> quoted or indirectly quoted someone who said :
>
> > Can anyone tell me how to
> >add an increment function to this so that each button will display a
> >different letter of the alphabet?

>
> just use a for loop:
>
> for ( char letter= 'A'; letter <= 'Z'; letter++ )


Java lets you use something other than "int" as a loop counter? I
thought it mostly just errored out at compile time if you tried that
type of thing, and most code I've seen to generate character values
numerically either adds a count to a char or looks up characters from
an array.

 
Reply With Quote
 
JackT
Guest
Posts: n/a
 
      08-01-2007
On Jul 31, 11:59 pm, Twisted <twisted...@gmail.com> wrote:
>
> > just use a for loop:
> > for ( char letter= 'A'; letter <= 'Z'; letter++ )

>
> Java lets you use something other than "int" as a loop counter?


Indeed.

You can even use Objects. For example, before Java 5,
the standard iterator pattern is this:

for(Iterator i = employeeList.iterator(); i.hasNext(); ) {
Employee x = (Employee) i.next();
...
}

 
Reply With Quote
 
Jim Korman
Guest
Posts: n/a
 
      08-01-2007
On Tue, 31 Jul 2007 20:22:49 GMT, Roedy Green
<> wrote:

>On Tue, 31 Jul 2007 13:03:56 -0700, wrote,
>quoted or indirectly quoted someone who said :
>
>> Can anyone tell me how to
>>add an increment function to this so that each button will display a
>>different letter of the alphabet?

>
>just use a for loop:
>
>for ( char letter= 'A'; letter <= 'Z'; letter++ )
>
>In Unicode you can count on the letters being in order and contiguous.
>see http://mindprod.com/jgloss/unicode.html

and then

add(new JButton(String.valueOf(letter));

Jim
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      08-01-2007
On Jul 31, 8:13 pm, JackT <jackt...@gmail.com> wrote:
> You can even use Objects. For example, before Java 5,
> the standard iterator pattern is this:


[snip]

Yes, of course I'm familiar with that. I just seem to recall it
balking on most primitive types other than "int", or at least throwing
off a torrent of warnings. C compilers usually also warn, at least on
use of char as a loop index. Which was exactly what was suggested
here.

And I'm fairly sure Java does choke on just about anything but "int"
for an array index, although that's moot if you don't use the loop
counter as an array index or do cast it to int. It definitely doesn't
permit longs as array indices.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
Increment Letters? Mike Javascript 3 12-05-2007 05:34 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM



Advertisments