java栈的应用_Java堆栈应用程序
我有一個(gè)Java問(wèn)題,涉及閱讀一個(gè)文本文件,并檢查它是否正確地平衡了花括號(hào),方括號(hào)和括號(hào) - '{','}','[',']','('和') ”。
讀取文件沒(méi)有問(wèn)題,但現(xiàn)在我應(yīng)該使用名為DelimPos的數(shù)據(jù)成員來(lái)保存行和字符,只要在讀取文件時(shí)找到其中一個(gè)分隔符,然后將其放入Stack中即可。然后我應(yīng)該通過(guò)堆棧并打印出任何錯(cuò)誤(即不平衡的分隔符,如'{[}')。
每當(dāng)我嘗試在主要方法中創(chuàng)建一個(gè)新的DelimPos d = new DelimPos(x, y)時(shí),它給了我這個(gè)錯(cuò)誤
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).
我不確定在此計(jì)劃中使用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");
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java栈的应用_Java堆栈应用程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 厦门的市鱼是什么?
- 下一篇: java美元兑换,(Java实现) 美元