我有一个Java问题,涉及到读取文本文件并检查它是否正确地平衡了大括号,方括号和括号 - '{','}','[',']','' ('和')'。Java Stack Application
我没有问题读取文件,但现在我应该使用名为DelimPos的数据成员来保存行和字符,只要我在读取文件时找到其中一个分隔符,然后将其放入Stack<DelimPos>
。然后,我应该通过堆栈并打印出任何错误(即不平衡的分隔符,如'{[}')。
每当我试着在你的主要方法的新DelimPos d = new DelimPos(x, y)
,它给了我这个错误
型BalancedApp没有封闭实例访问。必须符合 的分配,并带有一个封闭的BalancedApp类型实例(例如 x.new A(),其中x是BalancedApp的一个实例)。
我不确定在这个程序中使用DelimPos的最佳方法是什么。
任何帮助,将不胜感激。
这里是我的代码:
import java.util.Stack;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.BufferedReader;
public class BalancedApp {
Stack<DelimPos> s = new Stack<DelimPos>();
public class DelimPos
{
private int linecnt;
private char ch;
public DelimPos(int lineCount, char character)
{
linecnt = lineCount;
ch = character;
}
public char getCharacter()
{
return ch;
}
public int getLineCount()
{
return linecnt;
}
}
public static void main(String args[]) {
int lineCount = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a file name: ");
String inputFile = sc.next();
try{
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
int text;
System.out.print(lineCount + ". ");
while((text = reader.read()) != -1)
{
char character = (char) text;
if(character == '\n')
{
System.out.print(character);
lineCount++;
System.out.print(lineCount + ". ");
}
else System.out.print(character);
DelimPos d = new DelimPos(lineCount, character);
}
}
catch(IOException e){
System.out.println("File Not Found");
}
}
}
可能重复的[Java - 没有可以访问Foo类型的封闭实例](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian 2016-03-04 00:29:35