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