Created
October 4, 2017 21:33
-
-
Save Owenleo778/9bdced9430dbb972b246c0c5b4c2d04b to your computer and use it in GitHub Desktop.
The first week's challenge for Space Cadets - finding the name of someone based off of their ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.URL; | |
public class NameFinder { | |
public static void main(String[] args) throws Exception { | |
String iD; | |
String name = ""; | |
String currentLine; | |
BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("Please enter the ID of the user you wish to find: "); | |
iD = input.readLine(); | |
URL url = new URL("http://www.ecs.soton.ac.uk/people/" + iD); //Creates a URL using the ID given | |
BufferedReader readPage = new BufferedReader(new InputStreamReader(url.openStream())); //Opens up the webpage's HTML source code | |
while ((currentLine = readPage.readLine()) != null) { //Creates a loop continually goes through each line of the source code until the end | |
if (currentLine.contains("<title>")) { // after '<title>' in the code is the name of the person, followed by a '|' making their name easy to find | |
name = currentLine.substring(currentLine.indexOf(">") + 1,currentLine.indexOf("|") - 1); | |
if (name.equals("People")) { | |
name = "That ID is currently not in use by anyone"; | |
} | |
} | |
} | |
System.out.println(name); | |
readPage.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment