Results 1 to 19 of 19
  1. #1
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default [Solved]Help with If and Else statement

    So i have made a game. And at this point I want to edit a command, by adding some restrictions. Such as only characters with these values (2000, 2100, 2110, 2112) are allowed to access the command. So here is my code.
    PHP Code:
         } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000 && chr.getJob().getId() == 2100 && chr.getJob().getId() == 2110 && chr.getJob().getId() == 2112);
            {
                for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                    try {
                        ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                        chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                    } catch (NumberFormatException nfe) {
                        break;
                    } catch (NullPointerException npe) {
                        continue;
                                  }
                }
            }
           }else{ // if i annotate this out, the code works.
                chr.dropMessage("You must me an Aran to do this!");
    Well, all of the code seems to be working, except the fact that the next line of code:
    PHP Code:
    } else if (sub[0].equals("rebirth")) {
    Gets the 'else' without 'if' error
    and the ']' expected.
    I've re-arranged those lines so many times, but it keeps spitting out errors.
    Thank you!
    Last edited by Aionia; 03-13-2011 at 08:55 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Maybe I am missing it but I don't see the part of the code which checks for a skill names rebirth, also, do you intend for a character to need all those skill numbers?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Could you fix your indentation? And put the braces in on the same line as the start of the block ("if" or "for") instead of the semicolon. Once it's readable it may make a little more sense.

    I'm guessing the "rebirth" line is intended to have the same level of indentation as the "maxskills" line.

  4. #4
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    All the code runs together, and the numbers are the stages of the character. 2000(stage 1)
    2100(stage 2)
    Leveling up is what im saying, but i want only, those 4 character values to be able to access that command.
    The way if effects rebirth, is when i add the }else{ statement, the line goes red giving me the error i explained. (netbeans ide)

  5. #5
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Could you fix your indentation? And put the braces in on the same line as the start of the block ("if" or "for") instead of the semicolon. Once it's readable it may make a little more sense.

    I'm guessing the "rebirth" line is intended to have the same level of indentation as the "maxskills" line.
    You're right, about the last part, and what do you mean fix indentation.
    Such as this?
    PHP Code:
         } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000 && chr.getJob().getId() == 2100 && chr.getJob().getId() == 2110 && chr.getJob().getId() == 2112) {
                for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                    try {
                        ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                        chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                    } catch (NumberFormatException nfe) {
                        break;
                    } catch (NullPointerException npe) {
                        continue;
                                  }
                }
            }
           }else{
                chr.dropMessage("You must me an Aran to do this!");
                } else if (sub[0].equals("rebirth")) {
                if (chr.getLevel() > 199 || (chr.getJob().getId()>999 && chr.getJob().getId()<1999 && chr.getLevel()>119)) {
                    chr.doReborn();
                    chr.saveToDB(true);
                    for (MapleCharacter everyone : c.getChannelServer().getPlayerStorage().getAllCharacters()) {
                        everyone.dropMessage(0, chr.getName() + " has just rebirthed! Congratulations!");
                        }
                    } else {
                    chr.dropMessage("You must be at least level 200 or 120.");
                }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Gets the 'else' without 'if' error
    and the ']' expected.
    I've re-arranged those lines so many times, but it keeps spitting out errors.

    They're helpful messages! Don't try and out maneuver them by randomly changing things.


    Please reply quickly!

    (don't say that. i know you mean to be enthusiastic rather than pushy, but it may not be interpreted that way...)

  7. #7
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    @pbrockway2
    Okay, and i understand they're helpful. But whenever I try to fix it, it never really resolves.
    that's why i added // in front of }else{ and everything resolved. I need the else, for it to not work on other characters without those values. Right?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Good you've got the ;.

    You're right, about the last part, and what do you mean fix indentation.

    More like this:

    Java Code:
    } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000 && chr.getJob().getId() == 2100 && chr.getJob().getId() == 2110 && chr.getJob().getId() == 2112) {
            for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                try {
                    ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                    chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                } catch (NumberFormatException nfe) {
                    break;
                } catch (NullPointerException npe) {
                    continue;
                }
            }
        }
    }else{
        chr.dropMessage("You must me an Aran to do this!");
    } else if (sub[0].equals("rebirth")) {
        if (chr.getLevel() > 199 || (chr.getJob().getId()>999 && chr.getJob().getId()<1999 && chr.getLevel()>119)) {
            chr.doReborn();
            chr.saveToDB(true);
            for (MapleCharacter everyone : c.getChannelServer().getPlayerStorage().getAllCharacters()) {
                everyone.dropMessage(0, chr.getName() + " has just rebirthed! Congratulations!");
            }
        } else {
            chr.dropMessage("You must be at least level 200 or 120.");
        }  
    }

    As written you have elseif - else - elseif which makes no sense. Perhaps the else bit is supposed to go "higher up" where you check the job id. Just like the message you drop in the "rebirth" part in fact.
    Last edited by pbrockway2; 03-13-2011 at 07:30 AM.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Fixing the indentation may point out where you where your error is. Your if clause makes it so they need to be all those levels to move on. I think you may mean to use or instead of and.

  10. #10
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Perhaps the else bit is supposed to go "higher up" where you check the job id.
    What do you mean, could you show me where? I've only been coding two weeks.
    And the else-if-else-if-else-if
    usually go in harmony. But because of that one line they clash.

    Quote Originally Posted by sundae887
    I think you may mean to use or instead of and.
    I think you're right, how would i write that in instead of &&?
    Last edited by Aionia; 03-13-2011 at 07:46 AM.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The point about indentation is that you should use 4 (or fewer) spaces for each indented block. Spaces are better if it is going to show up in a web browser.

    Look at how you (correctly) handle the "rebirth" case:

    Java Code:
    } else if (sub[0].equals("rebirth")) {
        if (chr.getLevel() > 199 || (chr.getJob().getId()>999 && chr.getJob().getId()<1999 && chr.getLevel()>119)) {
            chr.doReborn();
            chr.saveToDB(true);
            for (MapleCharacter everyone : c.getChannelServer().getPlayerStorage().getAllCharacters()) {
                everyone.dropMessage(0, chr.getName() + " has just rebirthed! Congratulations!");
            }
        [color=red]} else {
            chr.dropMessage("You must be at least level 200 or 120.");
        }[/color]
    //end of elseif is here

    The other one should be similar, and not this:

    Java Code:
    } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000 && chr.getJob().getId() == 2100 && chr.getJob().getId() == 2110 && chr.getJob().getId() == 2112) {
            for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                try {
                    ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                    chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                } catch (NumberFormatException nfe) {
                    break;
                } catch (NullPointerException npe) {
                    continue;
                }
            }
        }  [color=blue]// the else belongs here under the if[/color]
    [color=red]}else{
        chr.dropMessage("You must me an Aran to do this!");[/color]
    // end of elseif should be here

    -----------------------

    Also don't forget about what sunde887 said about the if condition.

  12. #12
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000) {
    }else{
        chr.dropMessage("You must me an Aran to do this!");
            for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                try {
                    ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                    chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                } catch (NumberFormatException nfe) {
                    break;
                } catch (NullPointerException npe) {
                    continue;
                }
            }
        }
    I edited the java like that and it didn't have any errors. But when i tested it in game, any character could do it, regardless of job. Any way to fix that? And thanks guys for being such a great help :) It really means a lot.

    EDIT: wait i think i see my mistake.
    Java Code:
    } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000) {
    }else{
        chr.dropMessage("You must be an Aran to do this!"); } //i need to add that } there, or else it will go ahead and max them anyways
            for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                try {
                    ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                    chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                } catch (NumberFormatException nfe) {
                    break;
                } catch (NullPointerException npe) {
                    continue;
                }
            }
        }
    Last edited by Aionia; 03-13-2011 at 08:19 AM.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The line chr.getJob().getId doesn't seem to do anything in this code snippet. Also, while code like this

    Java Code:
    if(condition){
      //stuff
    } else if(condition){
      //more stuff
    }
    may take up a little less space it may make code a little more difficult to read.
    Java Code:
    if(condition){
      //stff
    }
    else if(condition){
      //stuff
    }
    This may be a bit easier to read. Of course ya just style preference but better readability will make finding problems easier
    Last edited by sunde887; 03-13-2011 at 08:20 AM.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    No - I didn't mean immediately under the if. I meant it should line up the way your "rebirth" block had the "if" and the "else" lining up.

    Java Code:
    } else if (sub[0].equals("rebirth")) {
        if (chr.getLevel() > 199 || (chr.getJob().getId()>999 && chr.getJob().getId()<1999 && chr.getLevel()>119)) {
        [color=green][b].[/b][/color]   chr.doReborn();
        [color=green][b].[/b][/color]   chr.saveToDB(true);
        [color=green][b].[/b][/color]   for (MapleCharacter everyone : c.getChannelServer().getPlayerStorage().getAllCharacters()) {
        [color=green][b].[/b][/color]       everyone.dropMessage(0, chr.getName() + " has just rebirthed! Congratulations!");
        [color=green][b].[/b][/color]   }
        } else {
            chr.dropMessage("You must be at least level 200 or 120.");
        }
    //end of elseif is here

  15. #15
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    So how should i line it up on the maxskills?
    Like so?
    Java Code:
     
    } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000) {
            for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                try {
                    ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                    chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                } catch (NumberFormatException nfe) {
                    break;
                } catch (NullPointerException npe) {
                    continue;
                }
            }
             }else{
            chr.dropMessage("You must be an Aran to do this!");
            }

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    We're getting closer! But you can't just paste lines in - you need to correct the indentation as you go...

    This is exactly the code you posted but with the indentation corrected so that the mistakes become clearer (indicated by the comments)

    Java Code:
    } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000) {
            for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                try {
                    ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                    chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                } catch (NumberFormatException nfe) {
                    break;
                } catch (NullPointerException npe) {
                    continue;
                }
            }  [color=blue]// this } shouldn't be here[/color]
            }else{
                chr.dropMessage("You must be an Aran to do this!");
            }
        [color=blue]// you are missing a } here[/color]
    // elseif ends here as before

  17. #17
    Aionia is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    It works! Thank you! I got it exactly how I wanted it. It only let's you max skills, if you are job id 2000, 2100, 2110, or 2112. So thank you very much! :)
    Java Code:
    } else if (sub[0].equals("maxskills")) {
        if (chr.getJob().getId() == 2000 || chr.getJob().getId() == 2100 || chr.getJob().getId() == 2110 || chr.getJob().getId() == 2112) {
            for (MapleData skill_ : MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/" + "String.wz")).getData("Skill.img").getChildren()) {
                try {
                    ISkill skill = SkillFactory.getSkill(Integer.parseInt(skill_.getName()));
                    chr.changeSkillLevel(skill, skill.getMaxLevel(), skill.getMaxLevel(), -1);
                } catch (NumberFormatException nfe) {
                    break;
                } catch (NullPointerException npe) {
                    continue;
                }
            }
             }else{
            chr.dropMessage("You must be an Aran to do this!");
            }
    Quote Originally Posted by pbrockway2
    } // this } shouldn't be here
    }else{
    chr.dropMessage("You must be an Aran to do this!");
    }
    // you are missing a } here
    Actually the way the other codes are around it, if i were to add another } and take away the other }. Then it wouldn't work =P
    But thanks again! It's working 100% now with 0 bugs.
    Last edited by Aionia; 03-13-2011 at 08:42 AM.

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Great - yes I think you're right. Anyway I'm glad you've got it going. And you discovered the ||.

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad you solved it, please mark your thread solved with the thread tools if you are done.

Similar Threads

  1. Replies: 2
    Last Post: 02-15-2011, 06:51 AM
  2. The if Statement, need some help!
    By Keno777 in forum New To Java
    Replies: 6
    Last Post: 10-24-2009, 01:53 AM
  3. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 03:56 PM
  4. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 03:58 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 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
  •