java stack 问题
我用stack类进行括号匹配问题。。代码如下importjava.util.*;publicclassexpTest{publicstaticvoidmain(Strin...
我用stack类 进行括号匹配问题。。代码如下
import java.util.*;
public class expTest{
public static void main(String[] args) throws Exception{
Stack<String> stack=new Stack<String>();
Scanner sc=new Scanner(System.in);
System.out.print("请输入一括号表达式:");
String str=sc.next();
String[] list=new String[str.length()];
for(int i=0;i<str.length();i++){
String ss=str.substring(i, i+1);
list[i]=ss;
}
for(int i=0;i<str.length();i++){
if((list[i].equals(new String("(")))||(list[i]).equals(new String("["))||(list[i].equals(new String("{"))))
stack.push(list[i]);
else if((list[i].equals(new String(")"))) && (stack.peek().equals(new String("("))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String(")"))) && (! stack.peek().equals(new String("(")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("]"))) && (stack.peek().equals(new String("["))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String("]"))) && (! stack.peek().equals(new String("[")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("}"))) && (stack.peek().equals(new String("{"))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String("}"))) && (! stack.peek().equals(new String("{")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String(")")))||(list[i].equals(new String("]")))||(list[i].equals(new String("}"))) && (stack.isEmpty())){
System.out.println("右括号多于左括号!");
}
}
if(! stack.isEmpty())
System.out.println("左括号多于右括号!");
else
System.out.println("括号匹配正确!");
}
}
运行,当我这样输入的时候:
请输入一括号表达式:{}}}}
Exception in thread "main" java.util.EmptyStackException
抛出了异常。。。请问这哪里出错了。。求助高手 展开
import java.util.*;
public class expTest{
public static void main(String[] args) throws Exception{
Stack<String> stack=new Stack<String>();
Scanner sc=new Scanner(System.in);
System.out.print("请输入一括号表达式:");
String str=sc.next();
String[] list=new String[str.length()];
for(int i=0;i<str.length();i++){
String ss=str.substring(i, i+1);
list[i]=ss;
}
for(int i=0;i<str.length();i++){
if((list[i].equals(new String("(")))||(list[i]).equals(new String("["))||(list[i].equals(new String("{"))))
stack.push(list[i]);
else if((list[i].equals(new String(")"))) && (stack.peek().equals(new String("("))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String(")"))) && (! stack.peek().equals(new String("(")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("]"))) && (stack.peek().equals(new String("["))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String("]"))) && (! stack.peek().equals(new String("[")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("}"))) && (stack.peek().equals(new String("{"))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String("}"))) && (! stack.peek().equals(new String("{")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String(")")))||(list[i].equals(new String("]")))||(list[i].equals(new String("}"))) && (stack.isEmpty())){
System.out.println("右括号多于左括号!");
}
}
if(! stack.isEmpty())
System.out.println("左括号多于右括号!");
else
System.out.println("括号匹配正确!");
}
}
运行,当我这样输入的时候:
请输入一括号表达式:{}}}}
Exception in thread "main" java.util.EmptyStackException
抛出了异常。。。请问这哪里出错了。。求助高手 展开
3个回答
展开全部
else if((list[i].equals(new String("}"))) && (! stack.peek().equals(new String("{")))){
stack.peek挂了。
你的判断有问题,第2个}stack已经empty了,你再调用stack.peek()就会挂,判断里的stack.peek().equals(new String。。。这个方法要用之前得先判断stack是否为空。给你改了下循环,后面的输出你看着改把,有问题发消息:
for(int i=0;i<str.length();i++){
if((list[i].equals(new String("(")))||(list[i]).equals(new String("["))||(list[i].equals(new String("{"))))
stack.push(list[i]);
else if((list[i].equals(new String(")"))) && (stack.peek().equals(new String("("))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String(")"))) && (! stack.isEmpty())&& (! stack.peek().equals(new String("(")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("]"))) && (! stack.isEmpty())&& (stack.peek().equals(new String("["))) ){
stack.pop();
}
else if((list[i].equals(new String("]"))) && (! stack.isEmpty())&& (! stack.peek().equals(new String("["))) ){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("}"))) && (! stack.isEmpty())&& (stack.peek().equals(new String("{"))) ){
stack.pop();
}
else if((list[i].equals(new String("}"))) && (! stack.isEmpty())&& (! stack.peek().equals(new String("{"))) ){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String(")")))||(list[i].equals(new String("]")))||(list[i].equals(new String("}"))) && (stack.isEmpty())){
System.out.println("右括号多于左括号!");
}
}
stack.peek挂了。
你的判断有问题,第2个}stack已经empty了,你再调用stack.peek()就会挂,判断里的stack.peek().equals(new String。。。这个方法要用之前得先判断stack是否为空。给你改了下循环,后面的输出你看着改把,有问题发消息:
for(int i=0;i<str.length();i++){
if((list[i].equals(new String("(")))||(list[i]).equals(new String("["))||(list[i].equals(new String("{"))))
stack.push(list[i]);
else if((list[i].equals(new String(")"))) && (stack.peek().equals(new String("("))) && (! stack.isEmpty())){
stack.pop();
}
else if((list[i].equals(new String(")"))) && (! stack.isEmpty())&& (! stack.peek().equals(new String("(")))){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("]"))) && (! stack.isEmpty())&& (stack.peek().equals(new String("["))) ){
stack.pop();
}
else if((list[i].equals(new String("]"))) && (! stack.isEmpty())&& (! stack.peek().equals(new String("["))) ){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String("}"))) && (! stack.isEmpty())&& (stack.peek().equals(new String("{"))) ){
stack.pop();
}
else if((list[i].equals(new String("}"))) && (! stack.isEmpty())&& (! stack.peek().equals(new String("{"))) ){
System.out.println("左右括号匹配不正确!");
return;
}
else if((list[i].equals(new String(")")))||(list[i].equals(new String("]")))||(list[i].equals(new String("}"))) && (stack.isEmpty())){
System.out.println("右括号多于左括号!");
}
}
展开全部
我没仔细看你的代码,但是有个问题题我要给你说一下。就是String类的subString(begin,end)方法截取的字符串是String索引位置从begin到(end-1)。你看看你的代码,像你那样截取实际上时没有截取的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码能不能写的可读性好点呢???
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询