2 Replies - 198 Views - Last Post: 24 November 2012 - 12:28 AM Rate Topic: -----

#1 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Brackets

Posted 23 November 2012 - 11:39 PM

private static void main error my try cannot execute without a catch method. I wanted the main method to work
in order to tell a user when they write a bracket the program will tell them that the brackets are balanced. If a
user enter the wrong letter or a symbol it should be displaying brackets not balanced at all.
explert programmer help me out?

public static void main (String [] args){
        boolean test = true;
        while(test) {   //user needs to enter a set of brackets to be tested for balance
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
            System.out.println("Please enter any set of brackets to be tested:  ");
            try{
                String input = reader.readLine();      
                boolean result = balancedBracketMethod (input);
                if (result) {
                       System.out.println("BALANCED");
                } else {
                       System.out.println("NOT BALANCED");
                }


public static boolean balancedBracketMethod (String s){
        int myLength = s.length();
        Stack st = new Stack();
        char d;
        int test = 0;
 
        for (int i=0;i<s.length();i++){   //loop to go through integers
            char c = s.charAt(i);
 
            if (c == '(')
            {
                st.push(new Character(c));
            }
            else if (c == '[') 
            {
                st.push(new Character(c));
            }
            else if (c == '{')
            {
                st.push(new Character(c));
            }
 
            else if (c == ')')
            {
                if (st.empty())
                {
                    return false;
                }
                else
                {
                    d = (Character) st.pop();
                    if (d != '(')
                    {
                        return false;
                    }
                }
            } 
            else if (c == ']'){
                if (st.empty()){
                    return false;
                }
                else{
                    d = (Character) st.pop();
                    if (d != '[')
                    {
                        return false;
                    }
                }
            }
            else if (c == '}'){
                if (st.empty())
                {
                    return false;
                }
                else
                {
                    d = (Character) st.pop();
                    if (d != '{')
                    {
                        return false;
                    }
                }
            }
        }
        if (st.empty()) 
        return true;
        else 
        return false;
    }
}
            }


This post has been edited by jon.kiparsky: 23 November 2012 - 11:55 PM
Reason for edit:: fixed [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Brackets

#2 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Brackets

Posted 23 November 2012 - 11:45 PM

Code tags would be nice.

And you need a catch statement. What a try/catch statement does is basically this.

Try to execute some code and if an error comes up (specify the error in your catch statement) then handle the error with your catch statement. All you are doing is trying to execute code, and disregarding catching the error.
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Brackets

Posted 24 November 2012 - 12:28 AM

nunc has it right: the syntax of a try/catch is like this:
try {
  // hazardous operation - file i/o, user input, or network connections, or
  // something else that can go wrong at run time
}
catch (SomeSpecificException sse)
{
  // handle the Exception here
}


One very important point is, do not ever do this:
catch (Exception e)
{
  
}


There are two errors here that should make your skin crawl when you see them. The first is, you're catching the generic Exception. This means that any exception thrown in the try block comes here - even if it's got nothing to do with the case you're handling. For example, you might have an object reference go sour on you, and throw a NullPointerException, when you thought you were handling file IO issues.
Worse yet, this code swallows the Exception, does nothing about it, and tries to carry on. This means you're short-circuiting the error reporting mechanism that tells you where the compiler thinks it ran into the error and what it thinks the error is. Obviously, this means debugging is a pain in the parts you sit on.

What you want to do is catch each Exception thrown by the methods you're calling, and deal with each one specifically. This is a place where "Don't repeat yourself" can give way to "it's okay to repeat yourself here". If you report the error to the user, log it, and quit, do that for each exception. It'll save you some debugging hell later on.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1