/** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 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("Enter URL: "); System.out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); String urlString = null; try { urlString = in.readLine(); url = new URL(urlString); } catch(MalformedURLException mue) { System.out.println(urlString + " is not valid.n" + "Try again."); getURL(); } catch(IOException ioe) { System.out.println("IOError when reading input: " + ioe); ioe.printStackTrace(); // Can skip return(null) now } finally { return(url); } }
import java.net.*; // For URL, MalformedURLException import java.io.*; // For BufferedReader /** A small class to demonstrate try/catch blocks. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class URLTest { public static void main(String[] args) { URLTest test = new URLTest(); test.getURL(); test.printURL(); } private URL url = null; /** Read a string from user and create a URL from it. If * reading fails, give up and report error. If reading * succeeds but URL is illegal, try again. */ public URL getURL() { if (url != null) { return(url); } System.out.print("Enter URL: "); System.out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); String urlString; try { urlString = in.readLine(); } catch(IOException ioe) { System.out.println("IOError when reading input: " + ioe); ioe.printStackTrace(); // Show stack dump. return(null); } try { url = new URL(urlString); } catch(MalformedURLException mue) { System.out.println(urlString + " is not valid.n" + "Try again."); getURL(); } return(url); } /** Print info on URL. */ public void printURL() { if (url == null) { System.out.println("No URL."); } else { String protocol = url.getProtocol(); String host = url.getHost(); int port = url.getPort(); if (protocol.equals("http") && (port == -1)) { port = 80; } String file = url.getFile(); System.out.println("Protocol: " + protocol + "nHost: " + host + "nPort: " + port + "nFile: " + file); } } }
/** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ // Simplified getURL method. public URL getURL() { if (url != null) { return(url); } System.out.print("Enter URL: "); System.out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); String urlString = null; try { urlString = in.readLine(); url = new URL(urlString); } catch(MalformedURLException mue) { System.out.println(urlString + " is not valid.n" + "Try again."); getURL(); } catch(IOException ioe) { System.out.println("IOError when reading input: " + ioe); ioe.printStackTrace(); // Show stack dump return(null); } return(url); }
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10218
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Java
Tags:Java/J2EE/J2MEBasic Java
Post Data:2017-01-02 16:04:23
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada