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

Need try catch block for number format. No doubles or letters, input must be an

ID: 3797023 • Letter: N

Question

Need try catch block for number format. No doubles or letters, input must be an integer.

Code********

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Recursion extends JFrame{
  
   //Set application width and height
   private final int APP_WIDTH=300;
   private final int APP_HEIGHT=300;
  
   private JLabel label=new JLabel("Enter number of dogs :");
   private JTextField txtField=new JTextField(10);
   private JLabel earsLabel=new JLabel("Number of ears :");
   private JButton btnCalculate=new JButton("Calculate");
   private JTextField outtxtField=new JTextField(10);
  
  
   //main method
   public static void main(String[] args) {  
       //instatiate the class
       new Recursion();      
   }
  
   //constructor to set default settings
   public Recursion() {
       //set size
       setSize(APP_WIDTH, APP_HEIGHT);
       //set title
       setTitle("Recursion");
       //set grid layout
       //1 2
       //3 4
       //5 6
       setLayout(new GridLayout(3, 2));
       //set text field of outtxtfield as false
       outtxtField.setEditable(false);
      
      
       //add controls to the frame
       add(label);add(txtField);
       add(earsLabel);add(outtxtField);
       add(new JLabel());add(btnCalculate);
      
       //add addactionlistener to the btnCalculate
       btnCalculate.addActionListener(new ButtonListener());
      
       pack();
       //set visible true for frame
       setVisible(true);
   }
  
   public class ButtonListener implements ActionListener {

       @Override
       public void actionPerformed(ActionEvent e) {
          
           //get text and convert to integer
           int numeras=Integer.parseInt(txtField.getText());
           //call method numOfEars method
           int totalears=numOfEars(numeras);
           //set outtxtField value          
           outtxtField.setText(String.valueOf(totalears));

       }
   }

   /*
   * The method numOfEars that takes n value and returns
   * number of ears as output
   * */
   public int numOfEars(int n)
   {
      
       if(n==0)
           return 0;
       //for even number add 4 ears
       else if(n%2==0)
           return 4+numOfEars(n-1);
       //for odd number add 2 ears
       else
           return 2+numOfEars(n-1);
   }

Explanation / Answer

Scanner input = new Scanner(System.in); int inputUser = -1; try { inputUser = input.nextInt(); } catch (InputMismatchException a) { System.out.print("Please provide only integer value as input"); } switch(inputUser){ case -1: //Control will come here if you didn't have any valid input. break;

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