Need iterator loop help.

Discussion in 'Spigot Plugin Development' started by DiscoDuck, Apr 17, 2017.

  1. So i'm making a plugin and I need the code to dispatch a command while a 2 string lists.hasNext(). the two lists have different numbers of names inside of them so the while code won't run when one of the lists runs out of names. And I dont want it to stop if string list #1 or list #2 is out of names but only if both of them are out of names. Does anyone know how i would go about doing this?
     
  2. You obviously need to add your own code as well
    Code (Text):
    while (list1.hasNext() && list2.hasNext()) {
       list1.next();
      list2.next();

    }[CODE]
     
  3. Here's the code that regards this:
    Code (Text):
              if (args[1].equalsIgnoreCase("confirm"))
              {
                  Iterator<String> it = allsubmits.iterator();
                  Iterator<String> i = world.iterator();
                  while(Stuff i need help on)
                  {
                      String submitting = it.next();
                      String worldz = i.next();
                      Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(),"lp user " + submitting + " permission unset worldguard.build.* global " + worldz);
                  }
     
  4. Code (Text):

     if (args[1].equalsIgnoreCase("confirm"))
              {
                  Iterator<String> it = allsubmits.iterator();
                  Iterator<String> i = world.iterator();
                  while(it.hasNext() && i.hasNext())
                  {
                      String submitting = it.next();
                      String worldz = i.next();
                      Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(),"lp user " + submitting + " permission unset worldguard.build.* global " + worldz);
                  }
     
    ... Wouldn't this be the solution then?
     
  5. i've tried that but. lets say "it" only has 1 name in it and "i" has 2. The command won't dispatch after it's read "it" because it only has 1 and it just read that one and now the && statement is now false because "it" doesn't have next.
     
  6. Make 2 Strings which are null
    Code (Text):
     while (! list1.hasNext() && !list2.hasNext()) {
    if (list1.hasNext()) {
      String last1 = list1.next();
    }

    if ( list2.hasNext()) {
     String last2 = list2.next();
    }
    //Run code here
    }
     
  7. Wouldn't you want to nest two for loops?
    Code (Java):
    for each world:
      for each player:
        dispatchCommand ...