loops and printouts on servlets

i know i can print out things to an applet... ie different images using a loop

for(int k = 0; k < 5; k ++)
{
  //g.drawImage(img1, int, int, this);
  //busyWait(PAUSE);
  //g.drawImage(img2, int, int, this),
  //busyWait(PAUSE);
}

but can this be done with other components and servlets

//**************use loop to print out radio buttons**************
                "<input type=\"radio\" name=\"color\" \n" +
                "value=\"Red\"> Red&nbsp;&nbsp; \n" +

                "<input type=\"radio\" name=\"color\"\n" +
                "value=\"Green\"> Green&nbsp;&nbsp;\n" +

                "<input type=\"radio\" name=\"color\"\n" +
                "value=\"Blue\" checked> Blue&nbsp;&nbsp;\n" +
//**************use loop to print out radio buttons**************

for(int k = 0; k < 5; k ++)
{
  //print out radiobuttons
}
annie613Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Yes it can be. Just print in the usual way but in a loop
0
annie613Author Commented:
i get an error when i put the for loop in my code
i think it has to do with where i am placing it

"<tr><td>Select a color:</td>\n" +
"<td>\n" +
                //it doesnt like where i put this for...illegal start of expression
                for(int k = 0; k < 3; k++)
                {
                   //**************use loop to print out radio buttons**************
                  "<input type=\"radio\" name=\"color\" \n" +
                  "value=\"Red\"> Red&nbsp;&nbsp; \n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Green\"> Green&nbsp;&nbsp;\n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Blue\" checked> Blue&nbsp;&nbsp;\n" +
                  //**************use loop to print out radio buttons**************

                }//end for
               
 "<tr><td>Select all the a number:</td>\n" +
 "<td>\n" +
0
annie613Author Commented:
and i know the code isnt correct yet
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

annie613Author Commented:
i see what i did. because i placed bad code in the for loop it thinks its illegal...now to code the loop
0
CEHJCommented:
Yes it is in the wrong place. But before fixing that: how would this benefit you? The values would only get repeated. You need distinct ones. You could do

name=\"color + k + \"

etc.

but is that what you'd want?
0
annie613Author Commented:
name=\"color + k + \"
yeah that is what i am looking to do..
:) let me play around :)
0
annie613Author Commented:
i am just trying to get a simple loop to print out

but i am not sure where to put the loop

for(int k = 0; k < 3; k++)
{
        out.println("test");
        out.println(k);
}//end for

i thought inside my doGet but i think im placing it in the wrong place?
0
CEHJCommented:
There's nothing wrong with that in itself, but of course i don't know the context. Before the loop followed this sort of thing:

"x" +

for (int i =

etc., which is clearly wrong. Make sure you have no String stuff before/after
0
annie613Author Commented:
its funny i can get the loop to work on one servlet but not another? let me try to exam the code a bit more *confused*
0
CEHJCommented:
btw, on a general point: you do realise that all this printing html from servlets is quite old hat and undesirable don't you? Although there are better ways, a small increment would take you to jsp which nonetheless would be a significant increment in ease of programming
0
annie613Author Commented:
i do understand that..i know it would be TOTALLY easier to use JSP seeing that one reason JSP was created was because of the challenges of using servlets to create forms like this..but i want to have an understanding of this before i move ahead...

im just confused on how to put the html into a java loop..
0
CEHJCommented:
>>but i want to have an understanding of this before i move ahead...

I understand, that's probably a good strategy for you. Here's an example.

for(int i = 0;i < 5;i++) {
      out.println("<input type=\"radio\" name=\"color" + i + "\n value=\"Red\">Red");
}
0
annie613Author Commented:
.
.
.
"<tr><td>Select color:</td>\n" +
"<td>\n");//i forgot to close this
               
                //test for a simple for loop
                //IT WORKS
                for(int k = 0; k < 3; k++)
                {
                    out.println(k);
                }//end for

//needed the out.println
out.println("<input type=\"radio\" name=\"status\" \n" +
"value=\"Sponsor\"> Sponsor&nbsp;&nbsp; \n" +

"<input type=\"radio\" name=\"status\"\n" +
"value=\"Member\"> Member&nbsp;&nbsp;\n" +

"<input type=\"radio\" name=\"status\"\n" +
"value=\"Visitor\" checked> Visitor&nbsp;&nbsp;\n" +

"<tr><td>Select Number:</td>\n" +
"<td>\n" +
.
.
.

now will the html be like a string??? im not sure i understand how this is a String and could be printed

"<input type=\"radio\" name=\"status\"\n" +
"value=\"Member\"> Member&nbsp;&nbsp;\n" +
0
annie613Author Commented:
something like this maybe

String[] colors = new String[] { "Red", "Blue", "Green"};

for (int i=0; i<colors.length; i++)
{
    //"<input type=\"radio\" name=\"color\" value=\"COLORNAME\"> COLORNAME&nbsp;&nbsp; \n"

    out.print("<input type=\"radio\" name=\"color\" value=\"");
    out.print(colors[i]);
    out.println("\">" + colors[i] + "&nbsp;&nbsp; \n");

}

im still trying :)
0
CEHJCommented:
But again, the above seems strange since you'd get 'Red' fives times. You probably need something more like

final String[] COLORS = { "Red", "Blue", "Green" };
for(int i = 0;i < COLORS.length;i++) {
      out.println("<input type=\"radio\" name=\"color" + i + "\n value=\"" + COLORS[i] + "\">" + COLORS[i]);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
I think you beat me to it ;-)
0
annie613Author Commented:
i think your code is more readable and solid so i chose to work with that..
my only problem is now making sure i have a radio checked when the page displays

my orginal code i had Blue checked

                  "<input type=\"radio\" name=\"color\" \n" +
                  "value=\"Red\"> Red&nbsp;&nbsp; \n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Green\"> Green&nbsp;&nbsp;\n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Blue\" checked> Blue&nbsp;&nbsp;\n" +

so im trying for that and i should have it
0
annie613Author Commented:
thanks again for all your help! i am glad we were able to both come up with something similiar. :) im going to work on the checked now and try to find more example to learn from! :) cheers
0
CEHJCommented:
// The below outside the loop
String part1 = "<input type=\"radio\" name=\"color" + i + "\n value=\"";
String part2 = ">" + COLORS[i];

// The below in the loop
if ("Blue".equals(COLORS[i])) {
      System.out.println(part1 + " checked" + part2);
}
else {
      System.out.println(part1 + part2);
}

0
CEHJCommented:
8-)

>>// The below outside the loop

Ignore that ;-)
0
annie613Author Commented:
awesome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.