A
A
Amir Kenesbay2021-01-26 16:54:54
Java
Amir Kenesbay, 2021-01-26 16:54:54

How to throw IllegalArgumentException if slot value is invalid?

I have a Player class and it has a shotWithWeapon method, I need to throw an IllegalArgumentException if the value of slot is invalid. And so that in the main method, when entering an incorrect value from the keyboard, it displays the corresponding exception.

public class PlayerMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Player player = new Player();

        System.out.println("Список оружии: " + "\n0.Пистолет\n1.Автомат\n2.Рпг\n3.Рогатка\n4.Водный пистолет\n");

        System.out.format("У игрока %d слотов с оружием,"
                        + " введите номер, чтобы выстрелить,"
                        + " -1 чтобы выйти:%n"
                , player.getSlotsCount()
        );

        int slot = scanner.nextInt();

        while (slot != -1) {
            switch (slot) {
                case 0:
                    player.shotWithWeapon(0);
                    break;
                case 1:
                    player.shotWithWeapon(1);
                    break;
                case 2:
                    player.shotWithWeapon(2);
                    break;
                case 3:
                    player.shotWithWeapon(3);
                    break;
                case 4:
                    player.shotWithWeapon(4);
                    break;
                default:

                    System.out.println("Выберите оружие от 0 до 4");
                    break;
            }
            slot = scanner.nextInt();
        }




        System.out.println("Game Over!");

    }
}


In the Player class, you need to throw an exception in the shotWithWeapon method

public class Player {
    // Указываем тип данных Weapon, который будет храниться в "слотах игрока"

    private Weapon[] weaponSlots;

    public Player() {
        weaponSlots = new Weapon[]{
                new Pistol(),
                new MachineGun(),
                new RPG(),
                new Slingshot(),
                new WaterGun()
        };
    }

    public int getSlotsCount() {
        return weaponSlots.length;
    }

    public void shotWithWeapon(int slot)  {
        //TODO проверить на выход за границы
        // Проверить на выход за границы
        // Выбросить IllegalArgumentException, если значение slot некорректно

        Weapon weapon = weaponSlots[slot];
        weapon.shot();
    }


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-01-26
@Amir1807

Relatively speaking, you need to check if the slot value passed to the shotWithWeapon() method is less than 0. Use the if construct. Next, it would be nice to know what data type is Slot. For example, if it is an array, then you need to check if the passed value is included in the array of values. Well, further inside the structure

public void shotWithWeapon(int slot)  {
if (slot < 0 || другие условия) {
throw new IllegalArgumentException();
}
        //TODO проверить на выход за границы
        // Проверить на выход за границы
        // Выбросить IllegalArgumentException, если значение slot некорректно

        Weapon weapon = weaponSlots[slot];
        weapon.shot();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question