well I'm pretty sure i took care of the error but whenever i try testing a string in the place of an integer it keeps sending an error although i specified to handle it.
i made the textField only accept integers, however i want it to display that if the string is entered it will send a JOptionPane saying only numbers are allowed.
here's my whole code :
public void giveItem() {
try {
String itemId = textField1.getText();
String itemAmount = textField2.getText();
if (isTargetEmpty()){
JOptionPane.showMessageDialog(
null, "Please enter a player to continue!", "Error", JOptionPane.INFORMATION_MESSAGE
);
} else if (itemId.isEmpty() || itemAmount.isEmpty()){
JOptionPane.showMessageDialog(
null,"Please fill the required fields!", "Error",JOptionPane.INFORMATION_MESSAGE
);
} else {
String player = getTargetName();
Player target = World.getPlayerByDisplayName(player);
if (target != null) {
int id = Integer.parseInt(itemId);
int amount = Integer.parseInt(itemAmount);
if (Double.isNaN(id) || Double.isNaN(amount)){
JOptionPane.showMessageDialog(
null, "You can only enter numbers!", "Error!", JOptionPane.INFORMATION_MESSAGE
);
} else if (amount <= target.getInventory().getFreeSlots()) {
target.getInventory().addItem(id, amount);
target.getPackets().sendGameMessage(
"You have received " + (amount > 1 ? " Items!" : "an Item!")
);
JOptionPane.showMessageDialog(
null, Utils.formatPlayerNameForDisplay(target.getUsername()) + " has received the " +
(amount > 1 ? "Items successfully in his/her inventory." : "Item successfully in his/her inventory.")
);
logAction(SPSutil.getTime() + Utils.formatPlayerNameForDisplay(target.getUsername())
+ " has received the item : " + id + ". Amount : " + amount + " in his/her inventory."
);
} else {
target.getBank().addItem(id, amount, true);
target.getPackets().sendGameMessage(
"You have received " + (amount > 1 ? "Items in your bank!" : "an Item in your bank!")
);
JOptionPane.showMessageDialog(
null, Utils.formatPlayerNameForDisplay(target.getUsername()) + " has received the " +
(amount > 1 ? "Items successfully in his/her bank." : "Item successfully in his/her bank.")
);
logAction(SPSutil.getTime() + Utils.formatPlayerNameForDisplay(target.getUsername())
+ " has received the item : " + id + ". Amount : " + amount + " in his/her bank."
);
}
} else {
JOptionPane.showMessageDialog(
null , "Player does not exist!", "Error" , JOptionPane.INFORMATION_MESSAGE
);
}
}
} catch(IOException e){
e.printStackTrace();
}
}
Double.isNaN()
will not check whether a number is valid after a parse. This method's argument is a double
(your id
is therefore automatically promoted) and it only checks whether this double
is Double.NaN
. Which no int
can ever be, even promoted.
What you must do is:
try {
int id = Integer.parseInt(itemId);
} catch (NumberFormatException e) {
// not an integer
}
Sure, you get it when invoke method Integer.parseInt(itemAmount);
. You should wrap it using try...catch
statement and handle this case properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.