increment letters

Discussion in 'Java' started by glennsnoise@hotmail.com, Jul 31, 2007.

  1. Guest

    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
    }
     
    , Jul 31, 2007
    #1
    1. Advertising

  2. Twisted Guest

    On Jul 31, 4:03 pm, 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));

    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.
     
    Twisted, Jul 31, 2007
    #2
    1. Advertising

  3. Roedy Green Guest

    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
     
    Roedy Green, Jul 31, 2007
    #3
  4. Roedy Green Guest

    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
     
    Roedy Green, Jul 31, 2007
    #4
  5. On Tue, 31 Jul 2007 20:16:37 +0000, Twisted wrote:

    > On Jul 31, 4:03 pm, 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.
     
    Joshua Cranmer, Jul 31, 2007
    #5
  6. Twisted Guest

    On Jul 31, 4:25 pm, Roedy Green <>
    wrote:
    > 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".


    Yeah, I figured it might need a cast in there somewhere.
     
    Twisted, Aug 1, 2007
    #6
  7. Twisted Guest

    On Jul 31, 4:22 pm, 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++ )


    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.
     
    Twisted, Aug 1, 2007
    #7
  8. JackT Guest

    On Jul 31, 11:59 pm, Twisted <> 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();
    ...
    }
     
    JackT, Aug 1, 2007
    #8
  9. Jim Korman Guest

    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
     
    Jim Korman, Aug 1, 2007
    #9
  10. Twisted Guest

    On Jul 31, 8:13 pm, JackT <> 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.
     
    Twisted, Aug 1, 2007
    #10
  11. JackT Guest

    On Aug 1, 1:47 am, Twisted <> wrote:
    > 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.


    You got that wrong.

    > C compilers usually also warn, at least on
    > use of char as a loop index.


    You got that wrong also.

    C code that deals with JPEG or GIF often uses
    "unsigned char" as array index to iterate through
    the 0..255 color range of each chroma, for example.

    And portable C code should use "size_t" (rather than "int")
    to iterate through memory. size_t is defined by C90 as
    being able to hold the largest object that could be allocated,
    so sizeof(size_t)<sizeof(int) on some platforms,
    and sizeof(size_t)>=sizeof(int) on others.

    > And I'm fairly sure Java does choke on just about anything but "int"
    > for an array index


    No one mentioned this. You like dragging discussions all
    over the place, often leading to +100 posts. Yeesh.
     
    JackT, Aug 1, 2007
    #11
  12. Twisted Guest

    On Jul 31, 9:57 pm, JackT <> wrote:
    [snip an assortment of utter crap]
    > No one mentioned this. You like dragging discussions all
    > over the place, often leading to +100 posts. Yeesh.


    You, meanwhile, apparently like violently attacking other posters
    without provocation, and making a formerly civil and technical
    discussion personal and adversarial.

    Which is worse, hmm, prick?

    Now go away and stop following up to my postings. If you absolutely
    must reply, confine the contents of your responses to purely technical
    matters and make no references to me or to any other persons save
    perhaps to yourself. If you are absolutely compelled, say at gunpoint,
    to respond in a personal and especially in a derogatory way USE E-
    MAIL!!! Thank you. :p

    Getting sick of this. I guess it's still Pick-on-Twisted Month for
    another two hours. :p
     
    Twisted, Aug 1, 2007
    #12
  13. JackT Guest

    On Aug 1, 2:06 am, Twisted <> wrote:
    > You, meanwhile, apparently like violently attacking
    > other posters...


    You can never silence me, you moron.

    I'll only stop correcting your mistakes
    when I've become bored with you, you pathetic bitch.
     
    JackT, Aug 1, 2007
    #13
  14. Twisted Guest

    On Jul 31, 10:09 pm, JackT <> wrote:
    > On Aug 1, 2:06 am, Twisted <> wrote:
    >
    > > You, meanwhile, apparently like violently attacking
    > > other posters...


    [vicious insults deleted]

    Go **** yourself, tard.

    I mean that literally. Find some decent porn (if you don't have any,
    STFW, numbskull, and if you are too brain-dead to find any genuine and
    free smut on the web, there's always bittorrent) and a quiet place and
    masturbate.

    Both of us will be much happier that way than if you continue to use
    your current method of keeping yourself amused and entertained.

    I suggest also making your newsreader open up to
    alt.binaries.pictures.erotica by default to help reinforce a
    preference for masturbation over usenet flaming as your primary means
    of entertainment.

    Do us all a favor now and go **** yourself.

    HTH. HAND.
     
    Twisted, Aug 1, 2007
    #14
  15. Twisted wrote:
    > On Jul 31, 8:13 pm, JackT <> 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.
    >


    I agree about longs as array indices, because the array index has to be
    convertible to int by unary numeric promotion, but char is fine.

    public class CharAsLoopIndex {
    public static void main(String[] args) {
    String[] array = new String[5];
    for(char c = 0; c < array.length; c++){
    array[c] = String.valueOf((char)(c+'A'));
    }
    }
    }

    Remember Java does not have a concept of a loop counter as a language
    structure.

    Patricia
     
    Patricia Shanahan, Aug 1, 2007
    #15
  16. JackT Guest

    On Aug 1, 2:17 am, Twisted <> wrote:
    > Go **** yourself...


    I guess that's all you've ever known... Ha!

    I don't take orders from you.
    Whenever you make a factual mistakes,
    I'll be there to point out your folly.
    So to stop my attacks, all you have to do is simple:

    (1) Stick to the facts.

    (2) Limit your non-facts chatter to
    a minimum; for example, in your discussion of Java,
    don't run off and slam COBOL. Your irrelevant
    junk is just so stinky, I can smell it without
    even logging on to Google Groups.

    You stink.

    Anyway, it's noon now, I'm off to lunch.
    Smell you later, you pathetic useless moron.
     
    JackT, Aug 1, 2007
    #16
  17. Twisted Guest

    On Jul 31, 10:22 pm, JackT <> wrote:
    [another steaming pile of off-topic, unconstructive, insulting
    bullshit]

    I'm sorry, but this is comp.language.java.programmer, not your
    personal toilet. Please dump your offal someplace else. Thank you;
    HAND.
     
    Twisted, Aug 1, 2007
    #17
  18. "Twisted" <> wrote in message
    news:...
    > 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.


    Anything that widens to int is a perfectly good index. That includes byte,
    char, and short.

    > It definitely doesn't
    > permit longs as array indices.


    Right; long -> int is a narrowing conversion, so it requires a cast.
     
    Mike Schilling, Aug 1, 2007
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    104
    Views:
    11,157
    Jordan Abel
    Oct 28, 2005
  2. Replies:
    99
    Views:
    2,558
    eliza81
    Jun 11, 2010
  3. Merrigan
    Replies:
    4
    Views:
    606
    Chris
    Dec 14, 2007
  4. Venugopal
    Replies:
    11
    Views:
    1,670
    Tassilo v. Parseval
    Nov 5, 2003
  5. Mike

    Increment Letters?

    Mike, Dec 5, 2007, in forum: Javascript
    Replies:
    3
    Views:
    113
Loading...

Share This Page