2012-02-03 61 views
2

我有一个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"); 
    } 


} 


} 
+0

可能重复的[Java - 没有可以访问Foo类型的封闭实例](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian 2016-03-04 00:29:35

回答

4

您已经定义DelimPos和主类BalancedApp内,以便将错误说,你需要的BalancedApp一个实例来实例化一个DelimPos。内部类别here有更好的解释。

但看着代码,我认为你根本不需要BalancedApp。我会删除它,并把你的主内DelimPos和你的堆栈内。像这样:

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


} 


} 
1

在你的情况下,你应该定义类为静态。

public static class DelimPos 
0

试试这个。

new BalancedApp.DelimPos(lineCount, character); 

但我建议您使用数据类型的单独实现,而不是将其用作内部类。

+1

这应该是'new BalancedApp()。new DelimPos(lineCount,character);' – dokkaebi 2012-02-03 03:49:37