Как продолжить l oop, даже если он ловит ошибку ArrayIndexOutOfBounds? - PullRequest
0 голосов
/ 04 февраля 2020

Если top == size, он ловит ошибку и отображает сообщение, что стек заполнен, не останавливая l oop. Как это сделать? Но мой код ниже не остановится

вот мой код ...

    while(ask==true){      
    try{
        String input=JOptionPane.showInputDialog("1.Push? Just type yes 2. Pop? just type no");
        if(input.equals("yes")){
            String element=JOptionPane.showInputDialog("Enter Element");
             int num=Integer.parseInt(element);
             top++;
          arr[top]=num;
             System.out.println("Insertion was successful:"+" "+arr[top]);
            // isFull();

        }else if(input.equals("no")){
            pop();

        }if(input.equals("exit")){
            getStack();
            top();
            ask=false;
        }
      }catch (ArrayIndexOutOfBoundsException e){
        System.out.println("full"); 
    }

}

1 Ответ

0 голосов
/ 04 февраля 2020
while(ask==true) {
try {
    String input=JOptionPane.showInputDialog("1.Push? Just type yes 2. Pop? just type no");
    if (input.equals("yes")){
        String element=JOptionPane.showInputDialog("Enter Element");
        int num=Integer.parseInt(element);
        top++;
        if (top == size) throw new ArrayIndexOutOfBoundsException(); //solution
        arr[top]=num;
        System.out.println("Insertion was successful:"+" "+arr[top]);
        // isFull();

    } else if (input.equals("no")){
        pop();
    } if (input.equals("exit")){
        getStack();
        top();
        ask=false;
    }
} catch (ArrayIndexOutOfBoundsException e){
    System.out.println("full");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...