|
-
Aug 16th, 2008, 08:34 PM
#1
Thread Starter
New Member
[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
-
Aug 17th, 2008, 03:33 AM
#2
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.
-
Aug 17th, 2008, 07:29 AM
#3
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
-
Aug 17th, 2008, 10:45 AM
#4
Re: [HELP]Java Exceptions
Avoid a one liner... check first if index 3 exists.
-
Aug 17th, 2008, 11:16 AM
#5
Thread Starter
New Member
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
-
Aug 17th, 2008, 11:22 AM
#6
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
-
Aug 17th, 2008, 11:37 AM
#7
Thread Starter
New Member
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
-
Aug 17th, 2008, 11:57 AM
#8
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
-
Aug 17th, 2008, 12:32 PM
#9
Thread Starter
New Member
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
-
Aug 17th, 2008, 04:16 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|