Hey guys im new to java in the process of learning how would i go buy adding exceptions to my code with a try/catch when i divide by zero or enter a word instead of and integer.

package errorapp;

/**
 *
 * @author Brandon
 */
import javax.swing.JOptionPane;
import java.util.InputMismatchException;
public class ErrorApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       //welcome
      System.out.println("Welcome to the ErrorApp  ");

 String firstNumber, //first string entered by user 
        secondNumber; // second string entered by user 

 int number1,     // first number to add 
     number2,    //second number to add 
     quotient;                //sum of number1 and number2 



 //read in the first number from user as a string 
firstNumber = 
        JOptionPane.showInputDialog ( "Enter first integer" );
//read in the second number from user as a string 
secondNumber = 
        JOptionPane.showInputDialog ( "Enter second integer" ); 

//convert numbers from type String to  int 
number1 = Integer.parseInt ( firstNumber); 
number2 = Integer.parseInt ( secondNumber); 

//add the numbers 
quotient = number1 / number2; 

//display the results 
JOptionPane.showMessageDialog (null,"The answer is  " + number1 + " divided by " 
                               + number2 + " equals " + quotient + ".", "Results", 
JOptionPane.PLAIN_MESSAGE); 



    }
}

Take a look at the tutorial at this link:
Click Here

This is what i came up with; it catches the error but i would like for it to catch the error after the first iput if the user enters "hello" instead of an integer, i want it to catch the error show the message dialog and the user be able to enter again for the first integer again.

package errorapp;
/**
 *
 * @author Brandon
 */
import javax.swing.JOptionPane;
public class ErrorApp {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       //welcome
      System.out.println("Welcome to the ErrorApp  ");
 String firstNumber, //first string entered by user 
        secondNumber; // second string entered by user 
 int number1,     // first number to add 
     number2,    //second number to add 
     quotient;                //sum of number1 and number2 


 try
 {
//read in the first number from user as a string 
firstNumber = 
        JOptionPane.showInputDialog ( "Enter first integer " ); 
//read in the second number from user as a string 
secondNumber = 
        JOptionPane.showInputDialog ( "Enter second integer" ); 
//convert numbers from type String to  int 
number1 = Integer.parseInt ( firstNumber); 
number2 = Integer.parseInt ( secondNumber); 
//divide the numbers 
quotient = number1 / number2; 
//display the results 
JOptionPane.showMessageDialog (null,"The answer is  " + number1 + " divided by " 
                               + number2 + " equals " + quotient + ".", "Results", 
JOptionPane.PLAIN_MESSAGE); 
 }
 catch ( NumberFormatException inputMismatchException )
 {
     JOptionPane.showMessageDialog(null, "You must enter an Integer ");
 }
catch ( ArithmeticException arithmeticException )
{
   JOptionPane.showMessageDialog(null, "Divide by zero error "); 
}




    }
}

user be able to enter again for the first integer again.

You will need to wrap a loop around your code that will loop back to where you ask the user for input if the input is invalid and to exit the loop if the input is valid. The code in the catch block can set the value of a variable to control whether the loop continues. If it is not set (good input) the loop exits. A while loop could be used for this.

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.