{"id":202702,"date":"2025-05-22T10:17:37","date_gmt":"2025-05-22T02:17:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202702\/"},"modified":"2025-05-22T10:17:37","modified_gmt":"2025-05-22T02:17:37","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e8%bf%9b%e8%a1%8c%e8%be%93%e5%85%a5%e9%aa%8c%e8%af%81%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202702\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>\u5728 Java \u4e2d\uff0c\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u9700\u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u548c\u9a8c\u8bc1\u65b9\u6cd5\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u4ee3\u8868\u9a8c\u8bc1\u5931\u8d25\u3002\u9a8c\u8bc1\u65b9\u6cd5\u5728\u9a8c\u8bc1\u5931\u8d25\u65f6\u629b\u51fa\u5f02\u5e38\u3002\u4f8b\u5982\uff1a\u9a8c\u8bc1\u7528\u6237\u59d3\u540d\uff0c\u901a\u8fc7 validateInput \u65b9\u6cd5\u68c0\u67e5\u662f\u5426\u4e3a\u7a7a\u6216\u5305\u542b\u7a7a\u683c\uff0c\u5e76\u76f8\u5e94\u629b\u51fa InvalidInputException\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729934320671cb3f0432c5.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u662f\u4e00\u79cd\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\u7684\u673a\u5236\u3002\u5728 Java \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u5e76\u786e\u4fdd\u5176\u7b26\u5408\u7279\u5b9a\u6807\u51c6\u3002<\/p>\n<p><strong>\u5f02\u5e38\u7c7b<\/strong><\/p>\n<p>\u8981\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u6765\u8868\u793a\u8f93\u5165\u9a8c\u8bc1\u5931\u8d25\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>InvalidInputException<\/code> \u7684\u5f02\u5e38\u7c7b\uff1a<\/p>\n<pre>public class InvalidInputException extends Exception {\n    public InvalidInputException(String message) {\n        super(message);\n    }\n}<\/pre>\n<p><strong>\u9a8c\u8bc1\u65b9\u6cd5<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u540d\u4e3a <code>validateInput<\/code> \u7684\u65b9\u6cd5\u4e2d\u7f16\u5199\u8f93\u5165\u9a8c\u8bc1\u903b\u8f91\uff0c\u8be5\u65b9\u6cd5\u5c06\u7528\u6237\u8f93\u5165\u4f5c\u4e3a\u53c2\u6570\u5e76\u629b\u51fa <code>InvalidInputException<\/code>\uff0c\u5982\u679c\u9a8c\u8bc1\u5931\u8d25\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>public static void validateInput(String input) throws InvalidInputException {\n    if (input.length() == 0) {\n        throw new InvalidInputException(\"\u8f93\u5165\u4e0d\u80fd\u4e3a\u7a7a\");\n    } else if (input.contains(\" \")) {\n        throw new InvalidInputException(\"\u8f93\u5165\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\");\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u59d3\u540d\u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u3002\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u59d3\u540d\uff0c\u5e76\u4f7f\u7528 <code>validateInput<\/code> \u65b9\u6cd5\u6765\u9a8c\u8bc1\u8f93\u5165\u662f\u5426\u6709\u6548\uff1a<\/p>\n<pre>import java.util.Scanner;\n\npublic class NameInputValidation {\n\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        System.out.print(\"\u8bf7\u8f93\u5165\u60a8\u7684\u59d3\u540d\uff1a\");\n        String name = scanner.nextLine();\n\n        try {\n            validateInput(name);\n            System.out.println(\"\u59d3\u540d\u5df2\u6210\u529f\u9a8c\u8bc1\u3002\");\n        } catch (InvalidInputException e) {\n            System.out.println(\"\u9519\u8bef\uff1a\" + e.getMessage());\n        } finally {\n            scanner.close();\n        }\n    }\n\n    public static void validateInput(String input) throws InvalidInputException {\n        if (input.length() == 0) {\n            throw new InvalidInputException(\"\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a\");\n        } else if (input.contains(\" \")) {\n            throw new InvalidInputException(\"\u59d3\u540d\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\");\n        }\n    }\n}<\/pre>\n<p>\u5728\u8fd0\u884c\u6b64\u7a0b\u5e8f\u65f6\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7a7a\u5b57\u7b26\u4e32\u6216\u5e26\u6709\u7a7a\u683c\u7684\u59d3\u540d\uff0c\u5c31\u4f1a\u629b\u51fa <code>InvalidInputException<\/code> \u5e76\u663e\u793a\u9519\u8bef\u4fe1\u606f\u3002\u5426\u5219\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa &#8220;\u59d3\u540d\u5df2\u6210\u529f\u9a8c\u8bc1\u3002&#8221;<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202702","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202702"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202702\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}