Solved Two for loops

Discussion in 'Plugin Development' started by KeybordPiano459, Dec 1, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    KeybordPiano459

    Hey, I'm working with two for loops that look something like this:
    Code:
    for (String product : plugin.getConfig().getStringList("products")) {
        for (int cost : plugin.getConfig().getIntegerList("cost")) {
            player.sendMessage(product + " - $" + cost);
        }
    }
    But since it goes through two lists, the message gets printed twice, how do I stop this?
     
  2. Offline

    adam753

    Why do you need to use for loops for this at all?
     
    ZeusAllMighty11 likes this.
  3. Offline

    KeybordPiano459

    Because I need as many messages printed as there are in the list in the config file, and I'm pretty sure that's the best way to do this.
     
  4. Offline

    Milkywayz

    It's probably be better to use config keys in this case.
    For example in config it'd look like this:
    PHP:
    Products:
      
    Product'Product Name'
        
    Cost1000
      Product
    'Product 2'
        
    Cost2000
    Then when you load the products, create a new Product object which holds the name and the cost along with other values.
     
  5. did you mean the following:
    Code:YAML
    1. Products:
    2. Product:
    3. Name: 'Product Name'
    4. Cost: 1000
    5. Product:
    6. Name: 'Product 2'
    7. Cost: 2000
    8.  

    because the config you posted is not valid YAMl acording to http://yaml-online-parser.appspot.com/.
     
  6. Offline

    Tirelessly

    Not if you're copying and pasting..
     
  7. I only checked it because I thought it would be invalid, and the example was about the yml structure, so I thought the creator of the thread was not so verry advanced whit yaml, so I given him an example what was valid yaml
     
  8. Offline

    KeybordPiano459

    ferrybig
    Well still how would I get what I need with this? Wouldn't I still have to check for two things?
     
  9. loop the keys that are inside the product tag, and get both config options from inside of it
     
  10. Offline

    KeybordPiano459

    ferrybig
    Like this?
    player.sendMessage(id + (product + ".name"));

    ferrybig
    Or this? =/ player.sendMessage(id + (plugin.getConfig().getString("products." + product + ".name")));

    Buuump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  11. Offline

    fireblast709

    Code:
    #name:costs
    Products:
      apple: 100
      diamond_sword: 200
    Code:java
    1. for(String product : plugin.getConfig().getKeys(false))
    2. {
    3. int cost = plugin.getConfig().getInt("Products."+product);
    4. player.sendMessage(product + " - $" + cost);
    5. }

    More compact than ferrybig and Milkywayz , while the config is still understandable
     
  12. Offline

    KeybordPiano459

    fireblast709
    What does .getKeys(false) do? Wouldn't it print everything in my config?
     
  13. Offline

    fireblast709

    getKeys(false) would get the children nodes of the path, not deep. Not deep means not deeper than 1 layer. For example the children of Products are all the product names in my example config (which is basically all you need as you get the cost from there)
     
  14. Offline

    KeybordPiano459

    fireblast709
    I figured out what that does, but it goes through the main config things, so it just prints 'products' not the things in products.

    EDIT: You ninja'd me :p But still it just goes through the main ones, not the products.
     
  15. Offline

    fireblast709

    Code:java
    1. for(String product : plugin.getConfig().getConfigurationSection("Products").getKeys(false))
    2. {
    3. int cost = plugin.getConfig().getInt("Products."+product);
    4. player.sendMessage(product + " - $" + cost);
    5. }
    Seems I forgot a getConfigurationSection()
     
  16. Offline

    KeybordPiano459

    SOLVED!

    Code:java
    1. for (String product : plugin.getConfig().getConfigurationSection("products.").getKeys(false)) {
    2. // yeeeaaaahhhh
    3. }


    fireblast709
    omfg you ninja'd me again

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page