Skip to content
Snippets Groups Projects
Commit f1bc0f7f authored by am13g23's avatar am13g23
Browse files

Commented, cleaned up code and deleted a bug

parent 0fd9a495
No related branches found
No related tags found
No related merge requests found
...@@ -4,46 +4,42 @@ import java.net.URL; ...@@ -4,46 +4,42 @@ import java.net.URL;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
class SotonLookupTool{ class SotonLookupTool{
public static String ReadLine(String message){ 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)); BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); //initialised buffered reader
System.out.print(message); System.out.print(message); //print message passed as a parameter
try { try {
return br.readLine(); return br.readLine(); //return the entered string
} catch (Exception e) { } catch (Exception e) { //if an exception occurs, print an error and return an empty string
System.out.println(e); System.out.println(e);
return ""; return "";
} }
} }
public static String MakeHttpGetRequest(String URL){
return "";
}
public static void main(String[] args){ public static void main(String[] args){
try { try {
String id = ReadLine("Enter id: "); 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); URL url = new URL("https://www.ecs.soton.ac.uk/people/" + id); //create the url, appendeding the ID
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //initialise the HTTP request, buffered reader and found flag
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
BufferedReader br= new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedReader br= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line; String line;
boolean found = false; 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(); line = br.readLine();
if (line != null){ if (line != null){ //test if null so we can use the .length method without causing an exception
if (line.length() >= 25){ 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
//System.out.println(line.substring(0,25)); 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
if (line.substring(0,25).equals("<meta property=\"og:title\"")){
System.out.print("Found: "); System.out.print("Found: ");
System.out.println(line.substring(35,(line.length()-4))); System.out.println(line.substring(35,(line.length()-4)));
found = true;
} }
} }
} }
} while (line != null); } 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"); System.out.println("Not found");
} }
} } //if at any point an exception occurs, it will be printed and the program will terminate
catch (Exception e) { catch (Exception e) {
System.out.println(e); System.out.println(e);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment