diff --git a/SotonLookupTool.java b/SotonLookupTool.java index cd54b8ad33e1376e24072726874cd41679e88245..07bafee5045d0e4afee29957dad65310ff9d5381 100644 --- a/SotonLookupTool.java +++ b/SotonLookupTool.java @@ -4,46 +4,42 @@ import java.net.URL; import java.net.HttpURLConnection; class SotonLookupTool{ - public static String ReadLine(String message){ - BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); - System.out.print(message); - try { - return br.readLine(); - } catch (Exception e) { + public static String ReadLine(String message){ //function to read the next line entered by the user in the console, after displaying a given message + BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); //initialised buffered reader + System.out.print(message); //print message passed as a parameter + try { + return br.readLine(); //return the entered string + } catch (Exception e) { //if an exception occurs, print an error and return an empty string System.out.println(e); return ""; } } - public static String MakeHttpGetRequest(String URL){ - return ""; - } - public static void main(String[] args){ try { - String id = ReadLine("Enter id: "); - URL url = new URL("https://www.ecs.soton.ac.uk/people/" + id); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + String id = ReadLine("Enter id: "); //get user to enter an ID to check + URL url = new URL("https://www.ecs.soton.ac.uk/people/" + id); //create the url, appendeding the ID + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //initialise the HTTP request, buffered reader and found flag connection.setRequestMethod("GET"); BufferedReader br= new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; boolean found = false; - do { + do { //do while loop to look through each line of the HTML file and extract the relevant info line = br.readLine(); - if (line != null){ - if (line.length() >= 25){ - //System.out.println(line.substring(0,25)); - if (line.substring(0,25).equals("<meta property=\"og:title\"")){ + if (line != null){ //test if null so we can use the .length method without causing an exception + if (line.length() >= 25){ //test if more than 25 chracters so we can check the substring of the first 25 characters without causing an exception + if (line.substring(0,25).equals("<meta property=\"og:title\"")){ //if the property value is correct, extract the relevant info and print it, setting the found flag System.out.print("Found: "); System.out.println(line.substring(35,(line.length()-4))); + found = true; } } } } while (line != null); - if (!found){ + if (!found){ //if the loop terminates and the name is not found, inform the user System.out.println("Not found"); } - } + } //if at any point an exception occurs, it will be printed and the program will terminate catch (Exception e) { System.out.println(e); }