Old Texas Code: URLExpSimple.java
What the code does: When you run the code, it prints out the HTML source code of the webpage with each new line being a token, in this case the chunks of code separated by whitespace. Then it counts the number of tokens, aka the number of lines, that it found.
How it works: First, the code creates a URL object "mySite" which points to the URL given in its parameter. Then, a connection to that URL is established by creating a URLConnection object "yc" and calling the URL method "openConnection()". After that, a Scanner object "in" is created which uses method "getInputStream()"" to get the data from "yc" (which is the HTML source code), and then "InputStreamReader" actually reads that data. So now the code is able to use "in" to parse through the source code, which the Scanner class automatically breaks down into tokens separated by whitespace. An int variable "count" is declared and initialized to 0 to keep track of how many tokens the code finds. The next part of the code is a while loop which continues checking the source code as long as "in.hasNext()" is true. // To clarify that further, "hasNext()" is a boolean method that checks whether "in" does or does not have whatever is specified, and in this case its checking for tokens separated by whitespace. // If true, then the code prints out the token, increases the count by 1, then goes back to the beginning of the loop until it is false. After the loop ends, the code prints out the number of tokens it found, which is stored in "count", and then the Scanner closes. All that code is contained within a "try" block, which runs as long as the code within it is valid. If it isn't and there's an error, the "catch" part of the block prints the exception using "printStackTrace()".