我的代码存在很多问题。我试图制作一个扫描文本文件的作业程序,并确保所有括号和括号(deliminator)匹配成对。我创建了一个DelimPos类并构建了一个构造函数。这成了我的堆栈" s"。每当我在文件中遇到一个开头的delim时,我就把它推到堆栈上。当我遇到一个结束delim时,我想将它与堆栈顶部进行比较,并在匹配时弹出堆栈。当我弹出堆栈时,它返回DelimPos类型,但不会从堆栈中删除它。
你能帮我弄清楚我做错了什么,并提出任何其他批评。只想变得更好。
public class BalancedApp {
private static class DelimPos {
private static int linecnt;
private static char ch;
public DelimPos(int lineCount, char character) { //constructor
linecnt = lineCount;
ch = character;
}
public static boolean match(char closeDelim) {
if (getCharacter() == '(' && closeDelim == ')') return true;
if (getCharacter() == '[' && closeDelim == ']') return true;
if (getCharacter() == '{' && closeDelim == '}') return true;
else return false;
}
public static char getCharacter() {
return ch;
}
public static int getLineCount() {
return linecnt;
}
}
public static void main(String[] args) {
//initialize stack
Stack<DelimPos> s = new Stack<DelimPos>();
int lineCount = 1;
//get input for file
System.out.printf("Enter file name: ");
String fileName = new Scanner(System.in).nextLine();
try{
//check for file,open, and scan it
Scanner inFile = new Scanner(new File(fileName));
inFile.useDelimiter("");
String text;
System.out.print(lineCount + ". ");
while(inFile.hasNext()){
text = inFile.next();
char character = text.charAt(0);
//-------NEW LINE-------
if(character == '\n') {
//System.out.print(character);
lineCount++;
System.out.print("\n"+lineCount + ". ");
}
//-------CHECK FOR OPEN DELIM AND ADD TO STACK IF TRUE-------
else if(character == '(' || character == '{' || character == '[') {
System.out.print(character);
s.push(new DelimPos(lineCount, character));
}
//-------CHECK FOR CLOSING DELIM, PRINT APPROPROATE ERROR, AND POP STACK
else if(character == ')' || character == '}' || character == ']') {
System.out.print(character);
//---WHEN THERE IS AN EXTRA CLOSING DELIM---
if (s.empty() == true) {
System.out.printf("\nError: Line %d. Closing character '%c', with no matching character.", lineCount, character);
System.exit(1);
}
boolean answer = DelimPos.match(character);
//---WHEN CLOSING DELIM DOES NOT BALANCE WITH OPENING DELIM---
if(answer == false) {
System.out.printf("\nError: Line %d. Symbol '%c' is the wrong closing symbol for char = '%c', line %d.", lineCount, character, DelimPos.getCharacter(), DelimPos.getLineCount());
System.exit(1);
}
else {
s.pop(); //<<<<<<<<<-----Where I'm having an issue
}
}
else {
System.out.print(character);
}
}
System.out.print("\nInput is balanced.");
inFile.close();
}
catch (FileNotFoundException e) {
System.out.printf("Unable to open file %s\n", fileName);
System.exit(1);
}
}
import java.util.Stack;
----下面编辑了代码,将静态对象更改为实例对象。 @JeffWard在下面解释.----
public class BalancedApp {
private static class DelimPos {
private int linecnt;
private char ch;
public DelimPos(int lineCount, char character) { //constructor
linecnt = lineCount;
ch = character;
}
public boolean match(char closeDelim) {
if (this.getCharacter() == '(' && closeDelim == ')') return true;
if (this.getCharacter() == '[' && closeDelim == ']') return true;
if (this.getCharacter() == '{' && closeDelim == '}') return true;
else return false;
}
public char getCharacter() {
return ch;
}
public int getLineCount() {
return linecnt;
}
}
public static void main(String[] args) {
int lineCount = 1;
//get input for file
System.out.printf("Enter file name: ");
String fileName = new Scanner(System.in).nextLine();
try{
//initialize stack
Stack<DelimPos> s = new Stack<DelimPos>();
//check for file,open, and scan it
Scanner inFile = new Scanner(new File(fileName));
inFile.useDelimiter("");
String text;
System.out.print(lineCount + ". ");
while(inFile.hasNext()){
text = inFile.next();
char character = text.charAt(0);
//-------NEW LINE-------
if(character == '\n') {
lineCount++;
System.out.print("\n"+lineCount + ". ");
}
//-------CHECK FOR OPEN DELIM AND ADD TO STACK IF TRUE-------
else if(character == '(' || character == '{' || character == '[') {
System.out.print(character);
s.push(new DelimPos(lineCount, character));
}
//-------CHECK FOR CLOSING DELIM, PRINT APPROPROATE ERROR, AND POP STACK
else if(character == ')' || character == '}' || character == ']') {
System.out.print(character);
//---WHEN THERE IS AN EXTRA CLOSING DELIM---
if (s.empty() == true) {
System.out.printf("\nError: Line %d. Closing character '%c', with no matching character.", lineCount, character);
System.exit(1);
}
else {
DelimPos data = s.pop();
boolean answer = data.match(character);
//---WHEN CLOSING DELIM DOES NOT BALANCE WITH OPENING DELIM---
if(answer == false) {
System.out.printf("\nError: Line %d. Symbol '%c' is the wrong closing symbol for char = '%c', line %d.", lineCount, character, data.getCharacter(), data.getLineCount());
System.exit(1);
}
}
}
else {
System.out.print(character);
}
}
if (s.empty() != true) {
DelimPos data = s.pop();
System.out.printf("\nError: At end of file, no closing symbol found for char = '%c', line %d.", data.getCharacter(), data.getLineCount());
System.exit(1);
}
else {
System.out.print("\nInput is balanced.");
inFile.close();
}
}
catch (FileNotFoundException e) {
System.out.printf("Unable to open file %s\n", fileName);
System.exit(1);
}
}
}
答案 0 :(得分:1)
我看到的问题是你的DelimPos
类是静态的,包括成员变量。遇到的每个分隔符都会更改堆栈中所有项目中ch
的值。
考虑以下输入{{()}}
遇到第一个(
时,前两个{
会被更改。像这样在DelimPos
添加toString():
@Override
public String toString() {
return "Delim: " + ch;
}
如果您在调试器中运行程序并在点击(
字符时检查堆栈,您将看到堆栈包含[Delim: (, Delim: (, Delim: (]