Results 1 to 10 of 10

Thread: [HELP]Java Exceptions

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    10

    [HELP]Java Exceptions

    Code:
    else if (splitted[0].equals("!removepoints")) {
                                        int repoints = Integer.parseInt(splitted[3]);
                                        victim.removePoints(repoints);
                                        mc.dropMessage(repoints+" points removed from "+victim.getName()+"'s account");
                                    }
    I need help! . Now the problem in the above code is if the user enters a different type of value in [splitted[3]] its gonna throw up a runtime error. Now i know im supposed to catch an exception, but which exception do i catch??
    Fact: GM Lallent believes snails are good for breakfast, lunch AND dinner? and sometimes afternoon snack.! =D << O_o"

    Fact2: People who quote famous celebrities, movie lines are ..... <= O_O

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: [HELP]Java Exceptions

    The Exception being thrown is probably NumberFormatException

    Enclose that part with a try and catch. Also you could check the stack trace what's the actual error that is being thrown.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [HELP]Java Exceptions

    I have a hunch that an ArrayIndexOutOfBoundsException is more likely to happen..
    Anyway, instead of catching the exception you need to be checking the input
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [HELP]Java Exceptions

    Avoid a one liner... check first if index 3 exists.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    10

    Re: [HELP]Java Exceptions

    Code:
     else if(splitted[0].equals("!addpoints")) {
                                        try { MapleCharacter victim = cserv.getPlayerStorage().getCharacterByName(splitted[1]);
                                        int points = Integer.parseInt(splitted[3]);
                                        victim.setPoints(points);
                                        mc.dropMessage(victim.getName()+" successfully credited with "+points+" points");
                                                     }   catch(IndexOutOfBoundsException smiles) {
                                                     mc.dropMessage("Syntax: !addpoints <Charname> <Amount of Points>");
                                                 }       catch(NumberFormatException ioerror) {
                                                     mc.dropMessage("A number should be given o.o");
                                                 }      
                                                 }
    So caught two exceptions. So shouldnt it show the Message if splitted[3] doenst exist and if the points amount is a non-integer value?
    Fact: GM Lallent believes snails are good for breakfast, lunch AND dinner? and sometimes afternoon snack.! =D << O_o"

    Fact2: People who quote famous celebrities, movie lines are ..... <= O_O

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [HELP]Java Exceptions

    I can think of a dozen or more exception that might be thrown, you need to check the user input instead of handling system exceptions
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    10

    Re: [HELP]Java Exceptions

    you mean something like
    Code:
    if(splitted.length > 3 || splitted.length< 3) {
    mc.dropMessage("Incorrect command syntax");
    } else {
    //code
    }
    this?
    Fact: GM Lallent believes snails are good for breakfast, lunch AND dinner? and sometimes afternoon snack.! =D << O_o"

    Fact2: People who quote famous celebrities, movie lines are ..... <= O_O

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [HELP]Java Exceptions

    Yes, something like that
    I don't know what you're trying to accomplish exactly. But checking user input is more effective than waiting for an Exception to be thrown
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    10

    Re: [HELP]Java Exceptions

    This is a game simulator written in Java. It takes in commands inputted by the user and proccesses them. This particular code enables gamemasters to give / remove points from users...

    ill try implementing that ^^
    Fact: GM Lallent believes snails are good for breakfast, lunch AND dinner? and sometimes afternoon snack.! =D << O_o"

    Fact2: People who quote famous celebrities, movie lines are ..... <= O_O

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [HELP]Java Exceptions

    Based on variable name splitted, I'll assume you used the split method which uses a regular expression as split criteria... if so, then you can use the matches method to check user input, e.g. you can check if a split group contains only numeric digits before actually performing the split.

Posting Permissions

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



Click Here to Expand Forum to Full Width