Skip to content
Snippets Groups Projects
Verified Commit 377c0854 authored by Emily Rowlands's avatar Emily Rowlands
Browse files

Added Java version. Does not work atm

parent dcd12ecf
Branches main
No related tags found
No related merge requests found
package jrr1g18.ecsnames;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ECSNames {
public static String getURLSource(URL url) throws IOException {
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
return result.toString();
}
public static String getNameFromSource(String source) {
Pattern regex = Pattern.compile("(?:\"name\">).*(?:<\\/h1)");
Matcher matcher = regex.matcher(source);
String grp = matcher.group();
return grp.substring("\"name\"".length(), grp.length() - "<\\/h1".length());
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
String uid = stdin.nextLine();
stdin.close();
URL url = null;
try {
url = new URL("https://www.ecs.soton.ac.uk/people/".concat(uid));
} catch (MalformedURLException e) {
e.printStackTrace();
System.exit(1);
}
String source = "";
try {
source = getURLSource(url);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
String result = getNameFromSource(source);
System.out.println(result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment