Results 1 to 8 of 8

Thread: Where do I add a 'finally' statement?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    19

    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.");
    			}
    
    		}
    
    	}
    }

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    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 {...}

  3. #3
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    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.
    Code:
     return choice;
    what is that for?

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Where do I add a 'finally' statement?

    Remember nothing can go between the try and catch clause... unless it's a comment.

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    19

    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.

  7. #7
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Where do I add a 'finally' statement?

    You mean, String strChoice = ""

    ?

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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
    "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
  •  



Click Here to Expand Forum to Full Width