Leap year checking and handling of digital conversion errors

Source: Internet
Author: User

leap year checking and handling of digital conversion errorsRequirements

Enter the year in the input box and click OK to show whether the year is a leap years.

Analysis

The following points should be paid attention to in the program writing

    1. The year must be after A.D., so the input year is not less than 0.
    2. The normative content of the input. The specification's input requirements are preceded by a number or minus sign, the remainder is a numeric string, and the range of numbers is limited by the computer's representation. The remaining inputs will be prompted for non-specification. So consider two aspects, one is the scope of the input specific data type representation range, because the actual year is difficult to exceed the range of [0, 2147483647], so it is appropriate to use the int type to represent the input year. The second is the characters in the input when the letter appears, either directly prompts for input errors, or as far as possible to convert the input characters into numbers. In specific implementations, the Try-catch mechanism can be used to catch errors, then handle errors, or to process data ahead of time to ensure data specification.
Specific implementation        Use Try-catch How to implement a catch error

This practice directly converts the input string, if the conversion succeeds, the program continues execution, continues to determine whether the year is a leap years, if the conversion fails, throws an error, the error is processed after the program continues to execute. It is important to note that the error in the conversion is only out of bounds, the string is empty and the string contains non-numeric three cases, but when the input negative is converted correctly, we need to change the number to a positive.

1  Public voidhandle (ActionEvent AE) {2         3         intYear = 0;4         Try {5Year =Integer.parseint (Textfield.gettext ());6             if(Year < 0){7Year *=-1;8Joptionpane.showmessagedialog (NULL,9"You should enter a positive number and now count as positive,"NULL,Ten joptionpane.warning_message); One  A             } -  -}Catch(Exception e) { theJoptionpane.showmessagedialog (NULL, -"Please enter the year with the number \ n range is [0, 2147483647]",NULL, - joptionpane.error_message); -             return; +         } -      +         if(Isleapyear (year)) { AJoptionpane.showmessagedialog (NULL, atYear + "Years are leap months",NULL, - joptionpane.information_message); -  -}Else{ -Joptionpane.showmessagedialog (NULL, -Year + "Years is common year",NULL, in joptionpane.warning_message); -         } to     } +      -  theHow to use Try-catch to catch errors
handling methods for catching errors using Try-catch

How to deal with data in advance

Processing the data ahead of time allows Integer.parseint () to accept strings that meet the requirements, which can also prevent errors from being generated. For programs that require user input, a better approach is to check the input as it is entered, which is more friendly than when the user input is played. Or, when the user submits the input, try to process the data. The following code is the practice of working with data as much as possible.

1      Public voidhandle (ActionEvent AE) {2String input =Textfield.gettext ();3         if(Input = =NULL|| Input.equals ("")) {4             return;5         }6         intYear = 0;7         8         if(Input.matches ("[0-9]{1,9}")) {9Year =integer.parseint (input); Ten}Else{ One             Char[] CHS =Input.tochararray (); A             inti; -              for(i = 0; i < chs.length && year < (INTEGER.MAX_VALUE/10); i++){ -                 if(Character.isdigit (Chs[i])) { theYear = (year*10 + integer.parseint ("" +chs[i])); -                 } -             } -             if(Year = = (INTEGER.MAX_VALUE/10) && i <chs.length) { +                  while(I <chs.length) { -                     if(Character.isdigit (chs[i]) && integer.parseint ("" + chs[i]) <= 7){ +Year = (year*10 + integer.parseint ("" +chs[i])); A                          Break; at                     } -i++; -                 } -             } -         } -         if(Isleapyear (year)) { inJoptionpane.showmessagedialog (NULL, year + "years are leap months",NULL, - joptionpane.information_message); to  +}Else { -Joptionpane.showmessagedialog (NULL, year + "Years is common year",NULL, the joptionpane.warning_message); *         } $     }Panax Notoginseng}
how to deal with data in advance

Summarize

In the specific environment, it is best to format the input data and make appropriate processing, because this will ensure the correct operation of the program, but also avoid the use of the Try-catch mechanism, because from the start of the try, the program needs to call more resources to do exception monitoring, This can result in a lot of resource usage. Of course, when committing a database transaction, the try cannot be omitted, because without a try, once the commit fails, there is no chance of rolling back.

Leap year checking and handling of digital conversion errors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.