Need Help With Try, Catch -new Student

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redpayne
    New Member
    • Oct 2006
    • 5

    #1

    Need Help With Try, Catch -new Student

    I am in my first year of Java programming and I am having a hard time. I have been working on a small program and probably have more than one mistake. My instructor told me to check where the try clause ends. I have no idea.

    I am new posting here so I am not sure if this will work. I hope you can copy and paste into a textpad file.
    Please please help.
    Thanks so much
    Code:
    
    import javax.swing.JOptionPane;
    
    public class MyType
    {
    
    	public static void main(String[] args)
    	{
    		// declare and construct variables
    		String strChoice, strTryString, strTryInt, strTryDouble;
    		int choice, tryInt;
    		double tryDouble;
    		boolean done = false;
    
    		// loop while user does not click cancel button
    		while(!done)
    		{
    		try
    				{
    				JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
    				choice = Integer.parseInt(strChoice);
    			}
    			switch(choice)
    				{
    				case 1:
    					JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
    					break;
    
    				case 2:
    					JOptionPane.showMessageDialog(null, "Correct");
    					tryInt = Integer.parseInt(strChoice);
    					break;
    
    				case 3:
    					JOptionPane.showMessageDialog(null, "Correct");
    					tryDouble = Integer.parseInt(strChoice);
    					break;
    
    				case 4:
    					done = true;
    					JOptionPane.showMessageDialog(null, "Now Closing");
    					break;
    
    				default:
    				throw new NumberFormatException();
    
    				}
    
    		}
    			}
    		catch(NumberFormatException e);
    				{
    				JOptionPane.showMessageDialog(null, "Please try again");
    				}
    		}
    
    
    
    		System.exit(0);
    
    	}
    
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Code:
    import javax.swing.JOptionPane;
    
    public class MyType
    {
    
    	public static void main(String[] args)
    	{
    		// declare and construct variables
    
    
    
    		int choice = 0;
    		int tryInt = 0;
    		double tryDouble;
    		boolean done = false;
    
    		// loop while user does not click cancel button
    		while(!done) {
    			try	{
    				String strChoice = JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
    				choice = Integer.parseInt(strChoice);
    				switch(choice) {
    					case 1:
    						JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
    						break;
    					case 2:
    						JOptionPane.showMessageDialog(null, "Correct");
    						//tryInt = Integer.parseInt(strChoice);
    						break;
    					case 3:
    						JOptionPane.showMessageDialog(null, "Correct");
    						//tryDouble = Integer.parseInt(strChoice);
    						break;
    					case 4:
    						done = true;
    						JOptionPane.showMessageDialog(null, "Now Closing");
    						break;
    					default:
    						throw new NumberFormatException();
    
    				}
    			}
    			catch(NumberFormatException e) {
    				JOptionPane.showMessageDialog(null, "Please try again");
    			}
    		}
    
    		System.exit(0);
    
    	}
    
    }
    Is there anything else that you wanted the program to do?

    Comment

    • redpayne
      New Member
      • Oct 2006
      • 5

      #3
      No, but I still can't get it to compile. It says class or interface expected. I appreciate your help. This stuff is so hard for me.
      Thanks,
      Kendra

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by redpayne
        No, but I still can't get it to compile. It says class or interface expected. I appreciate your help. This stuff is so hard for me.
        Thanks,
        Kendra
        Make sure there are no unintentional characters (dots, commas etc in your code). It does not give an error here

        Comment

        • redpayne
          New Member
          • Oct 2006
          • 5

          #5
          Ahh, There was a colon at the top. Thanks, In my instructions in the book, it tells me begin a while loop to repeat as long as the user does not hit the cancel button but does not tell how to end if they do. Would that be an action listener? It said to import the io package and I noticed you took it out. I didn't know why it said to import that either.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by redpayne
            Ahh, There was a colon at the top. Thanks, In my instructions in the book, it tells me begin a while loop to repeat as long as the user does not hit the cancel button but does not tell how to end if they do. Would that be an action listener? It said to import the io package and I noticed you took it out. I didn't know why it said to import that either.
            To make the program exit if the user clicks cancel, add this

            Code:
            String strChoice = JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
            		if(strChoice == null) {
            			strChoice = ""+4;
            		}
            If cancel is pressed, strChoice will be null, so set it to the exit option. The io thing is the reason I asked if you wanted to do something else because as you can see package io has nothing to do with this.

            Comment

            Working...