#1
  1. No Profile Picture
    =) wannabe?
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2002
    Location
    florida
    Posts
    2,153
    Rep Power
    14

    if statement and for loop


    i was just following this tutorial. and i cant get the exercise right

    they give two examples

    Code:
    <TABLE BORDER=2>
    <%
        for ( int i = 0; i < n; i++ ) {
            %>
            <TR>
            <TD>Number</TD>
            <TD><%= i+1 %></TD>
            </TR>
            <%
        }
    %>
    </TABLE>
    and

    Code:
    <%
        if ( hello ) {
            %>
            <P>Hello, world
            <%
        } else {
            %>
            <P>Goodbye, world
            <%
        }
    %>
    i get how the examples should work.

    but

    here's the exercise:

    "Exercise: Make the above examples work. Write a JSP to output all the values returned by System.getProperties with "<BR>" embedded after each property name and value. Do not output the "<BR>" using the "out" variable. "

    i dont get it
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    78
    Rep Power
    11
    Code:
    <%
    for (int i = 0; i < x.size(); i++) { 
    %>
      <%=x.getName()%>, <%=x.getValue()%><BR>
    <%
    } // END for (int i = 0; i < x.lenght; i++) 
    %>
    I believe they want you to output the name/value pares in their own scriptlet then just add the BR tag after you escape to the html area.

    Hope this helps.
    Last edited by Sm00ve; July 9th, 2003 at 08:42 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    78
    Rep Power
    11
    Double post ... sorry.
  6. #4
  7. No Profile Picture
    =) wannabe?
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2002
    Location
    florida
    Posts
    2,153
    Rep Power
    14
    but they never gave or told how to use size(), length or getValue or getName.

    and what's x?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    78
    Rep Power
    11
    If this is for a class then I might be helping you a little too much, but, the way I would work through the problem is like so...

    All of this information is in the javaDocs for the J2SDK.

    1. Lookup System.getProperties() in the java docs. Pay attention to the return type, it is a class called Properties.

    2. Lookup the class Properties and you see ALL of the avaialable methods. We need to figure out a way to get the Names/values out of the Properties object iterativly. So in the Properites javaDoc we see a method called .propertyNames() which returns a class called an Enumeration.
    Quoted from javaDoc:
    public interface Enumeration
    An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series.
    This looks like exactly what we need because once we get a list of the names (sometimes called keys) we can get the corresponding values out of the properties file by calling the Properties.getProperty(String name) method.

    With out writing the code here are the steps.
    1. Get the properties from System.getProperties(), and store it into a local Properties variable.
    2. Call Properties.getPropertyNames() on your local variable to get the Enumeration of the names so that you can loop though them.
    3. Set up a while loop to loop through the Enumeration.
    4. Inside of the loop call the Enumeration.nextElement() and store it into a local String variable to get the current iteration's name (don't forget to cast it as a String, the .nextElement() returns a type of object and the System properties are Strings).
    5. Call the Properties.getProperty(String name) where the name argument is the local String variable and store the returned value into another local string variable that represents the value.
    6. You are now ready to output the name/value pair like i said in my earlier post.

    If you are still confused I can post the code with comments. But i urge you to use the javaDocs and try to work it out your self. The process will help you figure other Java questions as you progress.
  10. #6
  11. No Profile Picture
    =) wannabe?
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2002
    Location
    florida
    Posts
    2,153
    Rep Power
    14
    ye that all makes sense.

    i just started out that day. and they never gave all the needed info. so i was just wondering.

    thanks a lot for explaining =) that cleared up a lot of other things.

IMN logo majestic logo threadwatch logo seochat tools logo