{"id":26958,"date":"2021-05-06T23:10:04","date_gmt":"2021-05-07T03:10:04","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/urltest-java-demonstrates-try-catch-blocks-programming-code-examples-java-j2ee-j2me-basic-java\/"},"modified":"2021-05-06T23:10:04","modified_gmt":"2021-05-07T03:10:04","slug":"urltest-java-demonstrates-try-catch-blocks-programming-code-examples-java-j2ee-j2me-basic-java","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26958","title":{"rendered":"URLTest.java Demonstrates try\/catch blocks. #Programming Code Examples #Java\/J2EE\/J2ME #Basic Java"},"content":{"rendered":"<pre>\n\/** Taken from Core Web Programming from \n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.  \n *\/\n \n \/\/ Further simplified getURL method. \n \n public URL getURL() {\n    if (url != null) {\n      return(url);\n    }\n    System.out.print(&quot;Enter URL: &quot;);\n    System.out.flush();\n    BufferedReader in = new BufferedReader(\n                          new InputStreamReader(System.in));\n    String urlString = null;\n    try {\n      urlString = in.readLine();\n      url = new URL(urlString);\n    } catch(MalformedURLException mue) {\n      System.out.println(urlString + &quot; is not valid.n&quot; +\n                        \t\t &quot;Try again.&quot;);\n      getURL();\n    } catch(IOException ioe) {\n      System.out.println(&quot;IOError when reading input: &quot; + ioe);\n      ioe.printStackTrace(); \/\/ Can skip return(null) now\n    } finally {\n      return(url);\n    }\n  }\n<\/pre>\n<pre>\nimport java.net.*; \/\/ For URL, MalformedURLException\nimport java.io.*;  \/\/ For BufferedReader\n\n\/** A small class to demonstrate try\/catch blocks.\n *\n *  Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\npublic class URLTest {\n  public static void main(String[] args) {\n    URLTest test = new URLTest();\n    test.getURL();\n    test.printURL();\n  }\n\n  private URL url = null;\n\n  \/** Read a string from user and create a URL from it. If\n   *  reading fails, give up and report error. If reading\n   *  succeeds but URL is illegal, try again.\n   *\/\n\n  public URL getURL() {\n    if (url != null) {\n      return(url);\n    }\n    System.out.print(&quot;Enter URL: &quot;);\n    System.out.flush();\n    BufferedReader in = new BufferedReader(\n                          new InputStreamReader(System.in));\n    String urlString;\n    try {\n      urlString = in.readLine();\n    } catch(IOException ioe) {\n      System.out.println(&quot;IOError when reading input: &quot; + ioe);\n      ioe.printStackTrace(); \/\/ Show stack dump.\n      return(null);\n    }\n    try {\n      url = new URL(urlString);\n    } catch(MalformedURLException mue) {\n      System.out.println(urlString + &quot; is not valid.n&quot; +\n                         &quot;Try again.&quot;);\n      getURL();\n    }\n    return(url);\n  }\n\n  \/** Print info on URL. *\/\n\n  public void printURL() {\n    if (url == null) {\n      System.out.println(&quot;No URL.&quot;);\n    } else {\n      String protocol = url.getProtocol();\n      String host = url.getHost();\n      int port = url.getPort();\n      if (protocol.equals(&quot;http&quot;) &amp;&amp; (port == -1)) {\n        port = 80;\n      }\n      String file = url.getFile();\n      System.out.println(&quot;Protocol: &quot; + protocol +\n                         &quot;nHost: &quot; + host +\n                         &quot;nPort: &quot; + port +\n                         &quot;nFile: &quot; + file);\n    }\n  }\n}\n<\/pre>\n<pre>\n\/** Taken from Core Web Programming from \n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.  \n *\/\n \n  \/\/ Simplified getURL method.\n  \n  public URL getURL() {\n    if (url != null) {\n      return(url);\n    }\n    System.out.print(&quot;Enter URL: &quot;);\n    System.out.flush();\n    BufferedReader in = new BufferedReader(\n                          new InputStreamReader(System.in));\n    String urlString = null;\n    try {\n      urlString = in.readLine();\n      url = new URL(urlString);\n    } catch(MalformedURLException mue) {\n      System.out.println(urlString + &quot; is not valid.n&quot; +\n                       \t\t\t &quot;Try again.&quot;);\n      getURL();\n    } catch(IOException ioe) {\n      System.out.println(&quot;IOError when reading input: &quot; + ioe);\n      ioe.printStackTrace(); \/\/ Show stack dump\n      return(null);\n    }\n    return(url);\n  }\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10218<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Basic Java<br \/>Tags:Java\/J2EE\/J2MEBasic Java<br \/> Post Data:2017-01-02 16:04:23<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * &copy; 2001 Marty Hall and Larry Brown; * may be freely used or adapted. *\/ \/\/ Further simplified getURL method. public URL getURL() { if (url != null) { return(url); } System.out.print(&quot;Enter URL: &quot;); System.out.flush(); BufferedReader in = &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26958\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26958","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10501,"url":"http:\/\/bangla.sitestree.com\/?p=10501","url_meta":{"origin":26958,"position":0},"title":"URLTest.java Demonstrates try\/catch blocks.","author":"","date":"August 29, 2015","format":false,"excerpt":"\/** Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0 \u00a0*\/ \u00a0 \u00a0\/\/ Further simplified getURL method. \u00a0 \u00a0public URL getURL() { \u00a0\u00a0\u00a0 if (url != null) {\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10194,"url":"http:\/\/bangla.sitestree.com\/?p=10194","url_meta":{"origin":26958,"position":1},"title":"UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.","author":"","date":"August 25, 2015","format":false,"excerpt":"UrlRetriever2.java\u00a0 Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; \/** Read a remote file using the standard URL class \u00a0*\u00a0 instead of connecting explicitly to the HTTP server. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26593,"url":"http:\/\/bangla.sitestree.com\/?p=26593","url_meta":{"origin":26958,"position":2},"title":"UrlRetriever2.java  Illustrates how the URL class can simplify communication to an HTTP server. #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; \/** Read a remote file using the standard URL class * instead of connecting explicitly to the HTTP server. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10192,"url":"http:\/\/bangla.sitestree.com\/?p=10192","url_meta":{"origin":26958,"position":3},"title":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes","author":"","date":"August 24, 2015","format":false,"excerpt":"UrlRetriever.java\u00a0 Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then \u00a0*\u00a0 passes these three values to the UriRetriever\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26489,"url":"http:\/\/bangla.sitestree.com\/?p=26489","url_meta":{"origin":26958,"position":4},"title":"UriRetriever.java  Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: #Programming Code Examples #NULL #Network Programming","author":"Author-Check- Article-or-Video","date":"April 26, 2021","format":false,"excerpt":"UriRetriever.java Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: import java.net.*; import java.io.*; \/** Retrieve a URL given the host, port, and file as three * separate command-line arguments. A\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26591,"url":"http:\/\/bangla.sitestree.com\/?p=26591","url_meta":{"origin":26958,"position":5},"title":"UrlRetriever.java  Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then * passes these three values to the UriRetriever\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26958"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26958\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26958"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}