Where do I add a 'finally' statement?
Code:
import java.io.*;
import javax.swing.JOptionPane;
public class MyType
{
public static void main(String[] args)
{
String strChoice, strTryString, strTryInt, strTryDouble;
int choice, tryInt;
double tryDouble;
boolean done = false;
while(!done)
{
try
{
String message = "What's My Type?" +
"\n\n1) String\n2) Integer\n3) Double\n4) Quit the program\n\n";
choice = Integer.parseInt(strChoice);
if (choice<1 || choice>3) throw new NumberFormatException();
else done = true;
}
switch(choice)
{
case 1:
System.out.println("You are correct");
break;
case 2:
choice = Integer.parseInt(strtryInt);
case 3:
choice = Double.parseDouble(strtryDouble);
case 4:
done = true;
}
return choice;
catch(numberFormatException e)
{
outputLabel.setText("Try Again.");
}
}
}
}
Re: Where do I add a 'finally' statement?
Not sure what you're up to but putting finally block is after catch(es)
try {...} catch (...) {} [...] finally {...}
Note the [...], catch can be one or more block on even a single try. Example would be
try {...} catch (...) {...} catch (...) {...} catch(...) {...} finally {...}
Re: Where do I add a 'finally' statement?
What are you trying to do? the catch block must follow by the try block. Ant the capital status of each viriable does metter unlike vb.
what is that for?
Re: Where do I add a 'finally' statement?
Remember nothing can go between the try and catch clause... unless it's a comment.
Re: Where do I add a 'finally' statement?
you have lots of problems:
1- This is a void (doesn't return a value)
2- try-catch are separated
3- you aren't using the break in each case of the switch
4- most vars are being used without initialization
5- you are throwing Exceptions without deinfing your main with a "throws"
6- you are importing java.io.* and not using it
Answering your question... you can add the finally statement after the catch(){}
Re: Where do I add a 'finally' statement?
Code:
import java.io.*;
import javax.swing.JOptionPane;
public class MyType
{
public static void main(String[] args)
{
String strChoice, strTryString, strTryInt, strTryDouble;
int choice, tryInt;
double tryDouble;
boolean done = false;
while(!done)
{
try
{
String message = "What's My Type?" +
"\n\n1) String\n2) Integer\n3) Double\n4) Quit the program\n\n";
choice = Integer.parseInt(strChoice);
if (choice<1 || choice>3) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Try Again.","Error",JOptionPane.INFORMATION_MESSAGE);
}
}
switch(choice)
{
case 1:
System.out.println("You are correct");
break;
case 2:
choice = Integer.parseInt(strTryInt);
break;
case 3:
choice = Integer.parseInt(strTryDouble);
break;
case 4:
done = true;
break;
}
}
}
How do I initialize my varibles?
I realize this is very very beginner stuff, but I have to learn somewhere.
Re: Where do I add a 'finally' statement?
You mean, String strChoice = ""
?
Re: Where do I add a 'finally' statement?
Quote:
Originally Posted by Squirrel RJ
How do I initialize my variables?
I realize this is very very beginner stuff, but I have to learn somewhere.
By giving an initial value to variables and calling constructors for objects