increment letters

G

glennsnoise

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
}
 
T

Twisted

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.
 
J

Joshua Cranmer

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.
 
T

Twisted

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.
 
T

Twisted

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.
 
J

JackT

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();
...
}
 
T

Twisted

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.
 
J

JackT

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.
 
T

Twisted

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
 
J

JackT

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.
 
T

Twisted

[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.
 
P

Patricia Shanahan

Twisted said:
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
 
J

JackT

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.
 
T

Twisted

[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.
 
M

Mike Schilling

Twisted said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top