需要处理堆栈数组的 ArrayIndexOutOfBoundsException,程序没有结束
Need to handle ArrayIndexOutOfBoundsException for stack array, without program ending
我正在做一个非常简单的程序,系统会提示用户输入最多 80 个字符。我们需要构建自己的堆栈并将每个字符压入堆栈。然后弹出并以相反的顺序显示字符。以为我已经完成了,但我的导师希望我在用户输入超过 80 个字符时做一些事情。基本上,我需要忽略所有超过 80 的字符。我该怎么做呢?我一直试图弄清楚这一点,但无法理解。我相信这将是我完全错过的简单事情。任何帮助,建议,不胜感激!
堆栈用户
导入 java.util.Scanner;
public class stackUser {
public static void main(String[] args){
System.out.println("\nPlease enter up to 80 characters and I will reverse them: ");
Scanner key = new Scanner(System.in);
String input = key.nextLine();
myStack stack = new myStack();
for(int i = 0; i < input.length(); i++){
char c = input.charAt(i);
stack.push(c);
}
if(stack.isEmpty()){
System.out.println("Stack is empty!");
}else{
while(!stack.isEmpty()){
char rev = stack.pop();
System.out.print(rev);
}
}
}
}
我的堆栈
public class myStack {
private int max = 80;
private char[] Stack = new char[max];
private int top = -1;
public void push(char input){
top++;
Stack[top] = input;
}
public char pop(){
char popped = Stack[top];
top --;
return popped;
}
public boolean isEmpty(){
boolean empty;
if(top == -1){
empty = true;
}else{
empty = false;
}
return empty;
}
}
围绕任何会抛出 IndexOutOfBounds 的事物的 try catch 循环
try {
...code here
}
catch (ArrayIndexOutOfBoundsException e) {
...whatever you want to do in event of exception
}
处理 ArrayIndexOutOfBoundsException 是个坏主意,您需要用 max
值检查当前 top
值。因为ArrayIndexOutOfBoundsException是unchecked exception,意味着开发者的错误。
我会像这样声明 push 方法,以表明如果达到最大值,它将抛出异常:
public void push(char input) throws ArrayIndexOutOfBoundsException{
top++;
Stack[top] = input;
}
然后在main方法中你可以使用一个try/catch块来处理异常:
try{
stack.push(c);
}catch (ArrayIndexOutOfBoundsException ex){
System.out.println("too much!");
}
我正在做一个非常简单的程序,系统会提示用户输入最多 80 个字符。我们需要构建自己的堆栈并将每个字符压入堆栈。然后弹出并以相反的顺序显示字符。以为我已经完成了,但我的导师希望我在用户输入超过 80 个字符时做一些事情。基本上,我需要忽略所有超过 80 的字符。我该怎么做呢?我一直试图弄清楚这一点,但无法理解。我相信这将是我完全错过的简单事情。任何帮助,建议,不胜感激!
堆栈用户 导入 java.util.Scanner;
public class stackUser {
public static void main(String[] args){
System.out.println("\nPlease enter up to 80 characters and I will reverse them: ");
Scanner key = new Scanner(System.in);
String input = key.nextLine();
myStack stack = new myStack();
for(int i = 0; i < input.length(); i++){
char c = input.charAt(i);
stack.push(c);
}
if(stack.isEmpty()){
System.out.println("Stack is empty!");
}else{
while(!stack.isEmpty()){
char rev = stack.pop();
System.out.print(rev);
}
}
}
}
我的堆栈
public class myStack {
private int max = 80;
private char[] Stack = new char[max];
private int top = -1;
public void push(char input){
top++;
Stack[top] = input;
}
public char pop(){
char popped = Stack[top];
top --;
return popped;
}
public boolean isEmpty(){
boolean empty;
if(top == -1){
empty = true;
}else{
empty = false;
}
return empty;
}
}
围绕任何会抛出 IndexOutOfBounds 的事物的 try catch 循环
try {
...code here
}
catch (ArrayIndexOutOfBoundsException e) {
...whatever you want to do in event of exception
}
处理 ArrayIndexOutOfBoundsException 是个坏主意,您需要用 max
值检查当前 top
值。因为ArrayIndexOutOfBoundsException是unchecked exception,意味着开发者的错误。
我会像这样声明 push 方法,以表明如果达到最大值,它将抛出异常:
public void push(char input) throws ArrayIndexOutOfBoundsException{
top++;
Stack[top] = input;
}
然后在main方法中你可以使用一个try/catch块来处理异常:
try{
stack.push(c);
}catch (ArrayIndexOutOfBoundsException ex){
System.out.println("too much!");
}