Results 1 to 7 of 7
  1. #1
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default For Loop Problem

    Hey anyone can check whats wrong with my checking with loop::
    Java Code:
     boolean found = false;
                                for(int j=0; j<role.length; j++){
                                     //if(count<1)
                                    found = false;
                                    {
                                                 
                                           out.write("<table border=\"1\"><tr><td>" + role[j].getName()+"</td><td></td></tr>");
                                                    //count++;
                                                      }
                                           for(int z=0;z<userName.size();z++){
                                     
                                              String u = userName.get(z).toString().substring( userName.get(z).toString().indexOf(",")+1);
                                        
                                                 if(u.trim().equals(role[j].getName().trim()))
                                                   {
                                            found = true;
                                                    out.write("<tr><td></td><td>"+userName.get(z).toString().substring(0,userName.get(z).toString().length()-u.length()-1)+"</td></tr></table>");
                                               
                                                }
                                            }
                                        }
    I need the out put like that:
    Java Code:
            MANAGERS
                    Tom
                    Jane
            EDITORS
                    Jane
                    Bill
    moderator edit: code tags added
    Last edited by Fubarable; 02-18-2010 at 06:23 PM. Reason: code tags added

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default

    And what output are you getting?

  3. #3
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Manager
    jane
    Manager
    Tom
    Editor
    Jane
    Bill

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    13

    Default

    Don't do any of this. Use a JSP to present the data.

  5. #5
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    i need to do in servlets, i dont use Jsp here 3rd party server requirements

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default

    Oh dear.
    You ought to tell them they're idiots then...or get a different supplier...or both.

  7. #7
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    10

    Default

    Well, assuming your stuck into doing things this way, the logical step would be to do a 'group by' operation, where we collect all the users by their role ahead of time and then with the structure in hand, do a 'for each role, print out each user in this role'.

    Java Code:
    Map<String, List<String>> namesByRole = new LinkedHashMap<String, List<String>>();
    
    // perform a 'group by' operation. 
    // finding all users and roles, group by role.
    for(int j=0; j<role.length; j++) { // we could use the java 1.5 for (Role aRole : roles) ?
      String roleName = role[j].getName();
      List<String> namesInRole = namesByRole.get(roleName);
      if (namesInRole == null) {
        namesInRole = new ArrayList<String>();
        namesByRole.put(roleName, namesInRole);
      }
    }
    for(int z=0;z<userName.size();z++) {
      Sting aUser = userName.get(z).toString()
      String usersRole = aUser.substring( aUser.indexOf(",")+1);
      String usersName = aUser.substring(0,aUser.length()-usersRole.length()-1);
      List<String> namesInRole = namesByRole.get(usersRole);
      if (namesInRole != null) {
        namesInRole.add(usersName);
      }
    }
    
    
    // arguably the above populated map should now be set into a request attribute, 
    // and even the JSP standard tag library <c:for> tag, yada yada
    
    out.write("<table border=\"1\">);
    for (String roleName : namesByRole.keySet()) {
      out.write("<tr><td>" + roleName +"</td><td></td></tr>");
      for (String userName : namesByRole.get(roleName) {
        out.write("<tr><td></td><td>" + userName + "</td></tr>");
      }
      out.write("</table>");
    }

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  2. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 01:15 AM
  3. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 09:30 AM
  4. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  5. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 10:24 PM

Posting Permissions

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