|
-
Feb 20th, 2006, 11:17 PM
#1
Thread Starter
Junior Member
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.");
}
}
}
}
-
Feb 20th, 2006, 11:23 PM
#2
Fanatic Member
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 {...}
-
Feb 21st, 2006, 06:32 AM
#3
Lively Member
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?
-
Feb 21st, 2006, 06:36 AM
#4
Frenzied Member
Re: Where do I add a 'finally' statement?
Remember nothing can go between the try and catch clause... unless it's a comment.
-
Feb 21st, 2006, 03:04 PM
#5
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(){}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Feb 22nd, 2006, 11:59 PM
#6
Thread Starter
Junior Member
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.
-
Feb 23rd, 2006, 01:19 AM
#7
Fanatic Member
Re: Where do I add a 'finally' statement?
You mean, String strChoice = ""
?
-
Feb 23rd, 2006, 07:53 AM
#8
Re: Where do I add a 'finally' statement?
 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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|