Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7405657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:26:13+00:00 2026-05-29T05:26:13+00:00

I have a Java problem that involves reading a text file and check that

  • 0

I have a Java problem that involves reading a text file and check that it has properly balanced curly braces, square brackets, and parentheses – ‘{‘, ‘}’, ‘[‘, ‘]’, ‘(‘, and ‘)’.

I have no problem reading the file, but now I am supposed to use a data member called DelimPos to hold onto the line and character whenever I find one of the delimiters while reading the file and then put it in a Stack<DelimPos>. I am then supposed to go through the stack and print out any errors (ie. unbalanced delimiters like ‘{ [ }’).

Whenever I try to make a new DelimPos d = new DelimPos(x, y) in the main method, it gave me this error

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).

I am unsure what the best way would be to use DelimPos in this program.

Any help would be appreciated.

Here is my code:

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


}


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:26:14+00:00Added an answer on May 29, 2026 at 5:26 am

    You’ve defined DelimPos and main inside the class BalancedApp, so as the error says, you need an instance of BalancedApp to instantiate a DelimPos. There is a better explanation of inner classes here.

    Looking at the code, though, I don’t think you need BalancedApp at all. I would remove it and put your main inside DelimPos and your stack inside main. Something like this:

    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");
        }
    
    
    }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently have a problem that my java code works perfectly ok on my
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I am using Struts and Java. The problem is that I have a page
I have a java server that has to communicate over a TCP socket to
I have a little Java problem I want to translate to Python. Therefor I
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
I'm having a small problem in Java. I have an interface called Modifiable. Objects
I have a problem using the Java search function in Eclipse on a particular
I have a problem with my Java progam suddenly exiting, without any exception thrown
I have a problem with loading image with java 2ME. I have a image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.