Click to See Complete Forum and Search --> : Where do I add a 'finally' statement?
Squirrel RJ
Feb 20th, 2006, 10:17 PM
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.");
}
}
}
}
nebulom
Feb 20th, 2006, 10:23 PM
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 {...}
chthong
Feb 21st, 2006, 05:32 AM
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.
return choice;
what is that for?
System_Error
Feb 21st, 2006, 05:36 AM
Remember nothing can go between the try and catch clause... unless it's a comment.
ComputerJy
Feb 21st, 2006, 02:04 PM
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(){}
Squirrel RJ
Feb 22nd, 2006, 10:59 PM
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.
nebulom
Feb 23rd, 2006, 12:19 AM
You mean, String strChoice = ""
?
ComputerJy
Feb 23rd, 2006, 06:53 AM
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.