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");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值