为什么我仍然收到NumberFormatException的错误?

时间:2014-03-25 09:55:11

标签: java numberformatexception

好吧我很确定我会处理错误但是每当我尝试在一个整数的位置测试一个字符串时它会一直发送错误,尽管我已指定处理它。

我让textField只接受整数,但我希望它显示如果输入字符串,它将发送一个JOptionPane,说只允许数字。

这是我的整个代码:

    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();
    }
}

2 个答案:

答案 0 :(得分:4)

Double.isNaN()检查解析后的数字是否有效。此方法的参数为double(因此会自动提升您的id),并且仅检查此double是否为Double.NaN。其中没有int,甚至是晋升。

你必须做的是:

try {
    int id = Integer.parseInt(itemId);
} catch (NumberFormatException e) {
    // not an integer
}

答案 1 :(得分:1)

当然,你在调用方法Integer.parseInt(itemAmount);时得到它。您应该使用try...catch语句对其进行包装并正确处理此情况。