Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Beginner: Append For Loop Integer

    Hello,

    I'm trying to streamline some code and having a little syntax issue. I want to change the original code:

    Code:
       
    case 5: 
           %><div class="cq4Col"><cq:include path="col1" resourceType="foundation/components/parsys"/></div>
           <div class="cq4Col"><cq:include path="col2" resourceType="foundation/components/parsys"/></div>
           <div class="cq4Col"><cq:include path="col3" resourceType="foundation/components/parsys"/></div>
           <div class="cq4Col"><cq:include path="col4" resourceType="foundation/components/parsys"/></div>
           <% break;
    ...
    To something that uses a for loop since the only thing that's different about each line is the "path" attribute. The code below compiles, but doesn't correctly append the number (i) to teh string.

    Code:
    case 5:
    for(int i=0 ; i < 5 ; i++)
        {
           %><div class="cq4Col"><cq:include path="col<%= Integer.parseInt(i) %>" resourceType="foundation/components/parsys"/></div>
        }
           <% break;
    ...
    In short, how do I append an integer to a string inside of quotes in Java?

  • #2
    New Coder
    Join Date
    Mar 2012
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts
    In short, how do I append an integer to a string inside of quotes in Java?
    In short, you should not touch this code without at least minimal knowledge of java.

    Technically you are trying to call Integer.parseInt on integer argument while it requires string. Method with required signature is not found and you fail.

    You want instead to simply write "i" or "Integer.toString(i)" instead.

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts
    RodionGork,

    Thanks for the commentary, but that didn't work either - "<%= Integer.toString(i) %>" is actually being written as part of the HTML - instead of "4". Same thing that was happening with parseInt.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •