我的代码有问题,请您帮我一个忙.. 我想进行递归从txt读取括号。 这是我的课程,因此,如果可能,我需要代码帮助,谢谢
import java.util.*;
import java.io.*;
public class MatchBrackets
{
public static void main(String [] args) throws FileNotFoundException
{
boolean answer;
String st;
Scanner infile = new Scanner (new FileReader("MatchExpression.txt"));
PrintWriter outFile = new PrintWriter ("MatchResults.txt");
while (infile.hasnext())
{
st = infile.nextLine();
outFile.println("Expression #" + ": " + st);
answer = matchBrackets(st);
if(answer == true)
outFile.println( " Invalid Expression \n");
else
outFile.println( " The value of this expression is: "
+ answer);
outFile.println(); outFile.println();
}
infile.close();
outFile.close();
}
public static boolean matchBrackets (String str)
{
ArrayStack S = new ArrayStack(100);
char ch;
if ((str == null) || str.isEmpty()) {
return str.isEmpty();
}
else {
ch=str.charAt(0);
if(ch=='{' || ch=='(' ||ch=='[' )
{ S.push(ch); }
else if (ch=='}' || ch==')' ||ch==']' ){
if(S.isEmpty())
return false;
switch(S.pop()){
case '{': if(ch!='}') return false; break;
case '[': if(ch!=']') return false; break;
case '(': if(ch!=')') return false; break;
}
}
return matchBrackets(str);
}
}
}
还有ArrayStack,包括POP,PUSH,isEmpty ...等等