简体   繁体   中英

Close a program if file being read isn't formatted properly?

My program reads a file the user chooses. Edit: The file contains, a 4 character String, 4 digit int, and char (In that order) on one line, with an undisclosed number of lines.

If one of these is formatted wrong or missing the program need to provide a error message and close.

How would I go about checking for the errors?

Thanks.

You can try parsing the content of the file within a try/catch bracket and if an exception is caught you display an error message and exit by with System.exit(0) ;

try {
    //parsing
}
catch (Exception e) {
    System.out.prinln("Error message");
    System.exit(0);
}

To achieve your goal you should read yourself into regular expressions and errorhandling in java.

Then you can check your input weather it is formatted properly by using regular expressions and finaly, if you need to, throw a self-defined exception that tells everyone what happened.

RegEx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM