خرید بک لینک

Vote count: 0

So my program asks the user to input two integers then if the user inputs a string it will display an error message "Invalid! Please input an integer". But after the error message shows up it displays an error.

This is the error but I managed to convert the JTextField to String then to Integer

 Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:580)
 at java.lang.Integer.parseInt(Integer.java:615)
 at SimpleCalculator1.main(SimpleCalculator1.java:52) 

And this is my code

import javax.swing.*;
public class SimpleCalculator1 
{

        public static void main(String [] args)
        {
              int error1, error2;
              JTextField field1 = new JTextField();
              JTextField field2 = new JTextField();
              JTextField field3 = new JTextField();
              Object[] message = 
                   {
                       "Enter first integer:", field1,
                       "Input second integer:", field2,
                       "Choose the operation to be usednAddition(+)nSubtraction(-)nMultiplication(*)nDivision(/):", field3,
                   };
             int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
             boolean shouldExit = false;
             while(!shouldexit)
                  {
                       if(option == JOptionPane.OK_OPTION)
                         {
                                String value1 = field1.getText();
                                String value2 = field2.getText();         
                                String operation = field3.getText();

                                try
                                    {
                                         int num1 = Integer.parseInt(value1);
                                         int num2 = Integer.parseInt(value2);
                                    }
                                catch(NumberFormatException ne)
                                    {
                                         JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
                                    }

                                switch(operation = field3.getText())
                                    {
                                       case "+":
                                       case "Addition":
                                             {
                                                  int num1 = Integer.parseInt(value1);
                                                  int num2 = Integer.parseInt(value2);
                                                  int result = num1+num2;
                                                  JOptionPane.showMessageDialog(null,"The sum is: "+result); 
                                                  break;
                                             }
                                       case "-":
                                       case "Subtraction":
                                             {
                                                  int num1 = Integer.parseInt(value1);
                                                  int num2 = Integer.parseInt(value2);
                                                  int result = num1-num2;
                                                  JOptionPane.showMessageDialog(null,"The difference is: "+result); 
                                                  break;
                                              }
                                        case "*":
                                        case "Multiplication":
                                              {
                                                  int num1 = Integer.parseInt(value1);
                                                  int num2 = Integer.parseInt(value2);
                                                  int result = num1*num2;
                                                  JOptionPane.showMessageDialog(null,"The product is: "+result); 
                                                  break;
                                              }
                                        case "/":
                                        case "Division":
                                              {
                                                  int num1 = Integer.parseInt(value1);
                                                  int num2 = Integer.parseInt(value2);
                                                  double result = (double)num1/num2;
                                                  JOptionPane.showMessageDialog(null,"The quotient is: "+result); 
                                                  break;
                                              }

                                       default: 
                                                  error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the begiing.
                         }
                       option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);

                  }
                       else if (option == JOptionPane.CANCEL_OPTION||)
                         {
                                JOptionPane.showMessageDialog(null,"Thank you for using our program!");
                                shouldExit = true;
                         }


       }
}

asked 1 min ago

- - , .

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 9 اسفند 1394 ساعت: 11:02

صفحه بندی