我有一个Java问题,涉及读取文本文件并检查其是否具有适当平衡的花括号,方括号和括号-'{','}','[[,']','('和') '。

我读取文件没有问题,但是现在我应该使用一个名为DelimPos的数据成员,只要在读取文件时找到定界符之一,然后将其放在Stack<DelimPos>中,就可以抓住行和字符。然后,我应该遍历堆栈并打印出所有错误(即,不平衡的定界符,例如“ {{}”)。

每当我尝试在main方法中创建新的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");
    }


}


}

最佳答案

您已经定义了DelimPos并将main定义在类BalancedApp中,因此如错误所示,您需要一个BalancedApp实例来实例化DelimPos。内部类here有一个更好的解释。

但是,查看代码,我根本不需要您使用BalancedApp。我将其删除并将您的main放入DelimPos并将您的堆栈放入main。像这样:

import java.util.Stack;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.BufferedReader;

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[]) {
    Stack<DelimPos> s = new Stack<DelimPos>();


    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 Stack应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9123067/

10-12 03:05