java高手,请看下里面的程序(括号匹配检测源文件程序),错在哪里,为什么是正确的文件结果却是错的
packagejackson.sjkh;importjava.io.*;importjavax.swing.*;publicclassTest006extendsJFra...
package jackson.sjkh;import java.io.*;
import javax.swing.*;public class Test006 extends JFrame{
private char myStack[];
private int top;
private int len;
private static JFileChooser fc;
private static File file =null;
private static String s; public Test006(){
fc = new JFileChooser();
fc.setDialogTitle("打开文件");
int result = fc.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
try{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
byte flush[] = new byte[1024];
int len =0;
while(-1!=(len=in.read(flush))){
s = new String(flush, 0, len);
System.out.println(s);
}
in.close();
}catch(FileNotFoundException e){
JOptionPane.showMessageDialog(null, "找不到文件!","异常",JOptionPane.ERROR_MESSAGE);
}catch(IOException e){
JOptionPane.showMessageDialog(null, "输入流异常","异常",JOptionPane.ERROR_MESSAGE);
}
}
}
public void stack(int len){
myStack = new char[len];
this.len = len;
top =-1;
} public boolean push(char value){
if(ifFull()){
return false;
}else {
myStack[++top] = value;
}
return true;
} public char pop(){
if(top<0){
return ' ';
}
return myStack[--top];
} public char peek(){
return myStack[top];
} public boolean ifFull(){
if(top==myStack.length-1){
return true;
}else {
return false;
}
} public boolean isEmpty(){
return top ==-1;
} public static void main(String [] args){
Test006 st = new Test006();
st.stack(s.length());
String result = "";
char c;
int i=0;
for(i=0;i<s.length();i++){
c = s.charAt(i);
switch(c){
case '{':
st.push(c);
break;
case '(':
st.push(c);
break;
case '[':
st.push(c);
break;
}
if(c=='}'|| c==')' || c==']'){
char c2 = st.pop();
result =result+c+"匹配"+c2+"\n";
if((c2!='{'&&c=='}')||(c2!='['&&c==']')||(c2!='('&&c==')')){
result =result+"错误\n";
break;
}
}
}
if(i==s.length()&&st.isEmpty()){
result =result+"全部匹配正确!\n";
}else{
result =result+"匹配错误!\n";
}
JOptionPane.showMessageDialog(null, result,"结果",JOptionPane.PLAIN_MESSAGE);
}
} 展开
import javax.swing.*;public class Test006 extends JFrame{
private char myStack[];
private int top;
private int len;
private static JFileChooser fc;
private static File file =null;
private static String s; public Test006(){
fc = new JFileChooser();
fc.setDialogTitle("打开文件");
int result = fc.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
try{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
byte flush[] = new byte[1024];
int len =0;
while(-1!=(len=in.read(flush))){
s = new String(flush, 0, len);
System.out.println(s);
}
in.close();
}catch(FileNotFoundException e){
JOptionPane.showMessageDialog(null, "找不到文件!","异常",JOptionPane.ERROR_MESSAGE);
}catch(IOException e){
JOptionPane.showMessageDialog(null, "输入流异常","异常",JOptionPane.ERROR_MESSAGE);
}
}
}
public void stack(int len){
myStack = new char[len];
this.len = len;
top =-1;
} public boolean push(char value){
if(ifFull()){
return false;
}else {
myStack[++top] = value;
}
return true;
} public char pop(){
if(top<0){
return ' ';
}
return myStack[--top];
} public char peek(){
return myStack[top];
} public boolean ifFull(){
if(top==myStack.length-1){
return true;
}else {
return false;
}
} public boolean isEmpty(){
return top ==-1;
} public static void main(String [] args){
Test006 st = new Test006();
st.stack(s.length());
String result = "";
char c;
int i=0;
for(i=0;i<s.length();i++){
c = s.charAt(i);
switch(c){
case '{':
st.push(c);
break;
case '(':
st.push(c);
break;
case '[':
st.push(c);
break;
}
if(c=='}'|| c==')' || c==']'){
char c2 = st.pop();
result =result+c+"匹配"+c2+"\n";
if((c2!='{'&&c=='}')||(c2!='['&&c==']')||(c2!='('&&c==')')){
result =result+"错误\n";
break;
}
}
}
if(i==s.length()&&st.isEmpty()){
result =result+"全部匹配正确!\n";
}else{
result =result+"匹配错误!\n";
}
JOptionPane.showMessageDialog(null, result,"结果",JOptionPane.PLAIN_MESSAGE);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询