AvailableJDK.java Can you please help me write the JAVA program to query http://
ID: 3573823 • Letter: A
Question
AvailableJDK.java
Can you please help me write the JAVA program to query
http://www.oracle.com/technetwork/java/javase/downloads/index.html
and display the first release number for a JDK listed on that page (and ONLY the release number). For example, at the time of this writing, the output would be
8u101
This summer (when our virtual machines were created), the release number output would have been
8u91
Your program should handle any thrown exceptions with an informative message.
===============
Example program:
HtmlReader.java
import javax.naming.MalformedLinkException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class HtmlReader {
public static void main(String[] args) {
String urlName = null;
if (args.length != 1) {
System.out
.println("Must have exactly 1 command line parameter that is a URL");
System.exit(4);
}
urlName = args[0];
Scanner scan = null;
URL webPage = null;
try {
webPage = new URL(urlName);
scan = new Scanner(webPage.openStream());
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (scan != null) scan.close();
scan = null;
}
/*
Create a URL object based on urlName (done--see above)
URL objects have an openStream() method that opens a connection
across a network and allows us to read whatever's at that specific location.
It returns an object of type InputStream
A Scanner can be created based on an InputStream object.
Once you have a Scanner object, use a while loop and hasNextLine and nextLine
to read the web page.
Use System.out.println to print each line
Use try/catch/finally to handle exceptions
Close the Scanner in the finally block
*/
}
}
Explanation / Answer
PROGRAM CODE:
package sample;
import javax.naming.MalformedLinkException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class HtmlReader {
public static void main(String[] args) {
String urlName = null;
if (args.length != 1) {
System.out
.println("Must have exactly 1 command line parameter that is a URL");
System.exit(4);
}
urlName = args[0];
Scanner scan = null;
URL webPage = null;
try {
webPage = new URL(urlName);
scan = new Scanner(webPage.openStream());
int i = 0;
while (scan.hasNextLine()) {
String line = scan.nextLine();
if(line.contains("Java Platform (JDK) "))
{
int index = line.indexOf("Java Platform (JDK) ");
line = line.substring(index, index+26);
line = line.replace("Java Platform (JDK) ", "");
System.out.println(line);
}
}
}
catch(FileNotFoundException e)
{
System.out.println("The mentioned URL was not found !");
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (scan != null) scan.close();
scan = null;
}
/*
Create a URL object based on urlName (done--see above)
URL objects have an openStream() method that opens a connection
across a network and allows us to read whatever's at that specific location.
It returns an object of type InputStream
A Scanner can be created based on an InputStream object.
Once you have a Scanner object, use a while loop and hasNextLine and nextLine
to read the web page.
Use System.out.println to print each line
Use try/catch/finally to handle exceptions
Close the Scanner in the finally block
*/
}
}
OUTPUT:
8u111
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.