Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help my assignment is to: Revisit your Facotrial GUI program and add try-ca

ID: 3689070 • Letter: N

Question

Need help my assignment is to:

Revisit your Facotrial GUI program and add try-catch statements to output an error message to the GUI if a user puts in bad input.

this is my code:

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Assignment7
{
   public static void main(String args[])
   {
       Scanner input = new Scanner(System.in);
      
       int number;
       int total = 1;
       String message;
      
       number = Integer.parseInt(JOptionPane.showInputDialog("Enter number (1 to stop): "));
      
       while(number != 1)
       {
           while(number > 1)
           {
              

           total = total * number;
           number--;
         
          
           }
       message = String.format("Total is: %d", total);
       JOptionPane.showMessageDialog(null, message);
      
       number = Integer.parseInt(JOptionPane.showInputDialog("Enter number (1 to stop): "));
      
       total = 1;
       }
      
   }

      
} //end class

Explanation / Answer

Program :

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Assignment7
{
   public static void main(String args[])
   {
       Scanner input = new Scanner(System.in);
      
       int number;
       int total = 1;
       String message;

try // Here the number is accepted and perform operation

{ // Opening brackets of try block

       number = Integer.parseInt(JOptionPane.showInputDialog("Enter number (1 to stop): "));
      
       while(number != 1)
       {
           while(number > 1)
           {

           total = total * number;
           number--;
           }
       message = String.format("Total is: %d", total);
       JOptionPane.showMessageDialog(null, message);      

}    // Closing brackets of try block

catch( Exception Number ) // If an exception rise in the program catch the excetion

{

System.out.println("Invalid Number!"); // Print Invalid Number

}

       number = Integer.parseInt(JOptionPane.showInputDialog("Enter number (1 to stop): "));
      
       total = 1;
       }
   }   

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote