java栈的应用_Java堆栈应用程序
我有一个Java问题,涉及阅读一个文本文件,并检查它是否正确地平衡了花括号,方括号和括号 - '{','}','[',']','('和') ”。
读取文件没有问题,但现在我应该使用名为DelimPos的数据成员来保存行和字符,只要在读取文件时找到其中一个分隔符,然后将其放入Stack中即可。然后我应该通过堆栈并打印出任何错误(即不平衡的分隔符,如'{[}')。
每当我尝试在主要方法中创建一个新的DelimPos d = new DelimPos(x, y)时,它给了我这个错误
No enclosing instance of type BalancedApp is accessible. Must qualify
the allocation with an enclosing instance of type BalancedApp (e.g.
x.new A() where x is an instance of 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 s = new Stack();
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栈的应用_Java堆栈应用程序的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 厦门的市鱼是什么?
- 下一篇: iText报表Java_(例)Java生