Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

exception handling try - catch

843789May 22 2009 — edited May 22 2009
Hi,
I am trying to use exception handling for the first time using try catch statements.
It seems that the try / catch is like a loop in the whatever variables are declared
within the try / catch - they are not accessible outside. Is this the case?

I am trying to use the try / catch here but I get the error
( at the "switch(token)" command ) that
my variable (token) cannot be found. Does anyone have any
suggestions how to resolve this? I don't wan't to
include the whole switch routine because there
are other input statements that I would like
to use try / catch with. Is there anyway to return / or retrieve the
hidden variable when you are outside of the try / catch?

Thanks,
M
public class PalindromeChecker {

     public static void main(String[] args) {
        
        QueueClass<String> strQueue =
                               new QueueClass<String>();
        
        Stack<String> strStack =
                               new Stack<String>(); 
        
        try
        {
        
        String str = JOptionPane.showInputDialog("To use the Palindrome Check "
                + "program enter\n1 for Strings\n2 for numbers\n0 to exit:\n");
            
        int token = Integer.parseInt(str);
        }
        catch (InputMismatchException aeRef)
        {
            JOptionPane.showMessageDialog(null, "Mismatch Exception!\n" +
                    "Please enter only a number", "Error", 
                     JOptionPane.PLAIN_MESSAGE);
        }
        
        switch(token){
             case 0: // to exit
                System.exit(0);
             case 1:
                String stringIn = JOptionPane.showInputDialog("Enter a string" +
                        " of letters");
                int count = 0;
                String s = "";
                String stringOut = "";
                char temp;
                        
                for(count = 0; count <= stringIn.length() - 1; count++) {
                    char c = stringIn.charAt(count);
                                                       
                    if(Character.isUpperCase(c)) {
                    temp = Character.toLowerCase(c);
                    s = String.valueOf(temp);
                    stringOut = stringOut.concat(s);    
                    strQueue.addQueue(s);
                    strStack.push(s);
                    } // end if
                else {
                    s = String.valueOf(c);
                    stringOut = stringOut.concat(s);
                    strQueue.addQueue(s);
                    strStack.push(s);
                } // end else
                    
                
                } // end for
                int i = 0;
                while(i <= stringIn.length()) {

                if(i == stringIn.length()){
                     JOptionPane.showMessageDialog(null, stringOut +" is " +
                     "a palindrome", "Result", 
                     JOptionPane.PLAIN_MESSAGE);
                     break;
                } // end if
                if(strQueue.front().equals(strStack.peek())) {
                    strQueue.deleteQueue();
                    strStack.pop();
                    i++;
                } // end if
                else if (!strQueue.front().equals(strStack.peek())) {
                    JOptionPane.showMessageDialog(null, stringOut +" is not " +
                    "a palindrome", "Result", 
                    JOptionPane.PLAIN_MESSAGE);
                    break;
                } // end else if
                } // end while
                break;
                
                case 2:
                    stringIn = JOptionPane.showInputDialog("Enter a number as" +
                        " an integer (i.e. no decimal places only digits");
                    count = 0;
                    s = "";
                    stringOut = "";
                
                        
                    for(count = 0; count <= stringIn.length() - 1; count++) {
                        char c = stringIn.charAt(count);
                                                       
                        s = String.valueOf(c);
                        stringOut = stringOut.concat(s);    
                        strQueue.addQueue(s);
                        strStack.push(s);
                    } // end for
                    
                    i = 0;
                    while(i <= stringIn.length()) {

                        if(i == stringIn.length()){
                            JOptionPane.showMessageDialog(null, stringOut +" is " +
                            "a palindrome", "Result", 
                            JOptionPane.PLAIN_MESSAGE);
                            break;
                            } // end if
                        if(strQueue.front().equals(strStack.peek())) {
                            strQueue.deleteQueue();
                            strStack.pop();
                            i++;
                        } // end if
                        else if (!strQueue.front().equals(strStack.peek())) {
                            JOptionPane.showMessageDialog(null, stringOut +" is not " +
                            "a palindrome", "Result", JOptionPane.PLAIN_MESSAGE);
                            break;
                } // end else if
                } // end while
        } // end switch
            
            
            

    } // end main
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 19 2009
Added on May 22 2009
2 comments
302 views