Code:if(playerCommand.startsWith("openshop")) { int shop = Integer.parseInt(playerCommand.substring(9)); c.getShops().openShop(shop); }
|
What's wrong with this? Compiled fine, but nothing happened when I used it. This is the first thing I've done from scratch so yeah...if (playerCommand.equalsIgnoreCase("openshop")) {
String[] args = playerCommand.split(" ");
if (args.length == 2) {
int shopToOpen = Integer.parseInt(args[1]);
c.getShops().openShop(shopToOpen);
} else {
c.sendMessage("Shop not found.");
}
}
Edit:
Tried this, still didn't work:
if (playerCommand.equalsIgnoreCase("openshop")) {
try {
String[] args = playerCommand.split(" ");
if (args.length == 2) {
int shopToOpen = Integer.parseInt(args[1]);
c.getShops().openShop(shopToOpen);
} else {
c.sendMessage("Shop not found.");
}
} catch(Exception e) {
}
}
Code:if(playerCommand.startsWith("openshop")) { int shop = Integer.parseInt(playerCommand.substring(9)); c.getShops().openShop(shop); }
Well the main reason was
The method equalsIgnoreCase in the String class only reads the words between the quotation marks.Code:if (playerCommand.equalsIgnoreCase("openshop")) {
This just checks if the string starts with that.Code:if(playerCommand.startsWith("openshop")) {
Also you shouldn't only use
if you are using a command with multiple values like pickup or tele, if it is a command that only has one value like this, :penshop 1, then you use the substring method.Code:playerCommand.split(" ");
so if the command isCode:playerCommand.substring(COMMAND_LETTER_AMOUNT + SPACE(1));
then you useCode:if(playerCommand.startsWith("height")) {
7 because height is 6 letters + 1 from the space.Code:String height = playerCommand.substring(7);
I disagree with you, Core
When you doyou are dividing the string by the space which would give you an array of strings. then for each for justCode:String[] cmd = playerCommand.split(" ");So much easier than substring.Code:cmd[integer]
also you should just use the
at the top and then you can useCode:String[] cmd = playerCommand.split(" ");
imo its a lot easier.Code:cmd[0].equalsIgnoreCase("another string");
Really no different, same exact outcome just using a different route.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |