I'm having trouble getting my InputMismatch working on my program. Do I need some kind of throw for this exception? The program compiles fine, but it dosen't pick up the exception. Can someone please help.

public class EndingListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		 if (e.getActionCommand().equals("Convert")){

        		try{
			     int base10;
			     String message, base2; 
					   
			     message = userField1.getText();
					   	
			     BaseConversion test = new BaseConversion();			
                       
                            base10 = Integer.parseInt( message );

                            base2 = test.stackreturn(base10);
                            userField2.setText(base2.toString());
                            } 
                    
        		catch ( InputMismatchException inputMismatchException ){         
       			                                                              
           				userField2.setText("Please Enter a Number");   
        				} 
            } 
                 
		 if (e.getActionCommand().equals("Clear"))
			{
                       userField1.setText("");
                       userField2.setText("");
                 }                  
                 
    }
}

Recommended Answers

All 5 Replies

Well, unless your BaseConversion.stackreturn() method throws that exception, nothing else in that code will.

It looks like you are actually wanting to catch NumberFormatException from the parseInt() call. Read the API doc on that method: http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
There's a lot of useful into in those docs - use them.

Thanks for the help I was able to get this to work. Is there a list on the Sun site that shows all the different types of exceptions?

Thanks for the help I was able to get this to work. Is there a list on the Sun site that shows all the different types of exceptions?

I would look up (in google) either--

java class Exception 6.0

or

java throwable 6.0

--and I say 6.0 because I assume you want some of the latest information.

From there you can study all of the possible Exceptions. To my knowledge, there should be over 30 of them.

Thanks for the help I was able to get this to work. Is there a list on the Sun site that shows all the different types of exceptions?

Any method that you call on the JDK API will document the exceptions that it can throw and the conditions under which they will be thrown. Those that extend Exception are "checked exceptions" and the compiler will make sure that you have try-catch blocks around those or that your method declares that it throws that exception. Exceptions that extend RuntimeException are considered errors. They are "unchecked exceptions" and the compiler does not require try-catch or "throws" declarations for those.

Got it, I'm starting to understand this a lot better. Thanks again!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.