Hello,

I am a complete beginner at Java and I'm having a problem with something I don't understand at all.

I'm trying to complete some homework in which I need to finnish off writing a small program which reads a text file and checks brackets in the file. Basically, it creates a stack, reads some file, and whenever it encounters an opening bracket "{", "(" or "[" it pushes it to the stack. Whenever it encounters a closing bracket it checks the top of the stack to see whether the opening bracket on top matches the closing bracket.

Whilst I believe I have the code to do that working, I'm getting an error each time I attempt to run it:

Error, args.length == 0

I guess the teacher was expecting this to happen as he wrote a piece of code in the main method to display it:

public class BracketChecker
{
    public static void main (String[] args)
    {
	 if (args.length == 0) {
		 System.out.println("Error, args.length == 0");
	 }
	    // use System.out to display an error message
	 else
	 {
		BracketCheck bc = new BracketCheck(args[0]);
	     System.out.println (bc.check());
	 }
    } // end main
} // end class

I won't put the whole code for the actual bracket checking as it's rather long (although if you need to see it I will).

So would anyone be able to help me out? I have a basic idea of what "args" is but I'm not sure how to fix this. I suspect it has something to do with needing to provide a file for it to read but I'm not sure, again, I'm a total beginner.

Sorry for the lengthy question, but I've been trying to figure it out for hours now.

Thanks in advance.

Recommended Answers

All 2 Replies

The point of the main method is providing some point of access for you program (making it runnable). If you are running from the command line you can run the java command:

> java BracketChecker [program arguments (args)]

otherwise if this is a GUI (Windowed) application use the javaw command instead of java.

Hope that helps.

The point of the main method is providing some point of access for you program (making it runnable). If you are running from the command line you can run the java command:

> java BracketChecker [program arguments (args)]

otherwise if this is a GUI (Windowed) application use the javaw command instead of java.

Hope that helps.

Thanks a lot!

Such a simple problem and solution...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.