Solved

for loop question

Posted on 2004-04-26
12
261 Views
Last Modified: 2010-03-31
Is this correct in that loop to add kp1.wav - kp50.wav tp the buttons

onOffButton.setActionCommand("kp" + (i + 1) + ".wav");


looprow : for (int row = 0; row < 20; row++)
          {
               for (int col = 0; col < 3; col++)
               {
                    int n = (20 * col + row + 1);
                    if (n > 50)
                    {
                         break looprow;
                    }
                    JButton onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(this);
                    onOffButton.setActionCommand("kp" + (i + 1) + ".wav");
                    gridPanel.add(onOffButton);
               }
          }
0
Comment
Question by:Drop_of_Rain
  • 6
  • 4
12 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 10916216
                  onOffButton.setActionCommand("kp" + (i + 1) + ".wav");

should be

                   onOffButton.setActionCommand("kp" + n + ".wav");

surely?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 10916223
for (int i=1; i<=50; i++)
{
    JButton onOffButton = new JColoredButton(Integer.toString(i), Color.green, Color.black);
    onOffButton.addActionListener(this);
    onOffButton.setActionCommand("kp" + i + ".wav");
    gridPanel.add(onOffButton);
}
0
 

Author Comment

by:Drop_of_Rain
ID: 10916246
objects
Date: 04/26/2004 12:51AM PDT
 Accepted Answer  


for (int i=1; i<=50; i++)
{
    JButton onOffButton = new JColoredButton(Integer.toString(i), Color.green, Color.black);
    onOffButton.addActionListener(this);
    onOffButton.setActionCommand("kp" + i + ".wav");
    gridPanel.add(onOffButton);
}
 
OK I can see the difference in the code, it does make more sense.

Thanks for the support,
Christopher
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 35

Expert Comment

by:TimYates
ID: 10916260
*sigh*
0
 

Author Comment

by:Drop_of_Rain
ID: 10916398
objects  TimYates answer was correct to fit the code I posted isn't that correct. I need to pay better attention. I want to be fair about this. I really try to do this.
 
onOffButton.setActionCommand("kp" + n + ".wav");



looprow : for (int row = 0; row < 20; row++)
          {
               for (int col = 0; col < 3; col++)
               {
                    int n = (20 * col + row + 1);
                    if (n > 50)
                    {
                         break looprow;
                    }
                    JButton onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(this);
                    onOffButton.setActionCommand("kp" + (i + 1) + ".wav");
                    gridPanel.add(onOffButton);
               }
          }


Comment from TimYates
Date: 04/26/2004 12:51AM PDT
 Comment  


                  onOffButton.setActionCommand("kp" + (i + 1) + ".wav");

should be

                   onOffButton.setActionCommand("kp" + n + ".wav");

surely?
 
Accepted Answer from objects
Date: 04/26/2004 12:51AM PDT
 Accepted Answer  


for (int i=1; i<=50; i++)
{
    JButton onOffButton = new JColoredButton(Integer.toString(i), Color.green, Color.black);
    onOffButton.addActionListener(this);
    onOffButton.setActionCommand("kp" + i + ".wav");
    gridPanel.add(onOffButton);
}
 
0
 

Author Comment

by:Drop_of_Rain
ID: 10934157
modulo:  Thank you very much.
0
 

Author Comment

by:Drop_of_Rain
ID: 10934179
I asked for this question to be reopened again. Because Tim Yates answered the question more precise to the code I posted. I have doubled the points to reward both of you for your answers.

Thanks for all the support,
Christopher
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10936619
Wow!!  Thanks Christopher!! :-)

I wish you all the luck in your project!!

Thanks again!!

Tim
0
 

Author Comment

by:Drop_of_Rain
ID: 10936682
You are welcome to share your knowledge with me by answering my questions any time you get a chance to.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10936779
:-)  will do :-)
0
 

Author Comment

by:Drop_of_Rain
ID: 10936793
Thanks, I have them up all the time
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 159
Java Timer (static) 9 43
jdbc error in jsp application 20 44
Chrome and Firefox Java 5 27
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question