Java programming- DO NOT USE BREAK METHOD Goals The lab this lesson introduces s
ID: 3826875 • Letter: J
Question
Java programming- DO NOT USE BREAK METHOD
Goals
The lab this lesson introduces students to File I/O and exception processing. By the end of this lab, students should be able to
Read and write text files
Process input from a file
Read web pages
Catch exceptions
Question-
Write a 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.
Explanation / Answer
Hi,
Please see the below classes. Please comment for any queris/feedbacks.
Thanks.
WebpageReader.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;
import java.util.StringTokenizer;
public class WebpageReader {
public static void main(String [] args){
try {
//Connecting to the URL
URL url = new URL("http://www.oracle.com/technetwork/java/javase/downloads/index.html");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
String relaeseNumber ="";
while ((str = in.readLine()) != null) {
//Reading each string on a line
StringTokenizer token = new StringTokenizer(str);
while(token.hasMoreElements()){
relaeseNumber= token.nextToken();
//Checking each word, if it starts with '8u' and '8u' should be starting
//and length should be less than 10
if(relaeseNumber.contains("8u") && relaeseNumber.indexOf("8u") ==0
&& relaeseNumber.length()<10){
//Removing html tags from the releaaseNumber
int trailIndex = relaeseNumber.indexOf("<");
String trail = relaeseNumber.substring(trailIndex);
relaeseNumber = relaeseNumber.replace(trail, "");
System.out.println("Release number for a JDK :: "+relaeseNumber);
}
}
}
in.close();
}
catch(IOException ex) {
// there was some connection problem, or the file did not exist on the server,
// or your URL was not in the right format.
// think about what to do now, and put it here.
ex.printStackTrace(); // for now, simply output it.
}
}
}
Sample output:
Release number for a JDK :: 8u131
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.