file and token parsing in 1.4.2 (compared to 1.5)
I am trying to implement a program that parses a simple file of urls, and spits out each of there path components. This program is written in SDK 1.5 (its from a textbook), but I am trying to re-do it as an exercise for 1.4.2. I am wanting to know if I am doing this properly.
Example: file "urls.inp" contains:
www.google.com
java.sun.com/j2se/1.5
www.linux.org/info/gnu.html
duke.csc.villanova.edu/lewis/
www.csc.villanova.edu/academics/index.jsp
Should produce output:
URL: www.google.com
www.google.com
URL: java.sun.com/j2se/1.5
java.sun.com
j2se
1.5
URL: www.linux.org/info/gnu.html
www.linux.org
info
gnu.html
URL: duke.csc.villanova.edu/lewis/
duke.csc.villanova.edu
lewis
URL: www.csc.villanova.edu/academics/index.jsp
www.csc.villanova.edu
academics
index.jsp
The program as written for SDK 1.5:
import java.util.scanner;
import java.io.*;
public class URLDissector {
public static void main (String[] args) throws IOException {
String url;
Scanner fileScan, urlScan;
fileScan = new Scanner (new File("urls.inp"));
while(fileScan.hasNext()) {
url = fileScan.nextLine();
System.out.println("URL: " + url);
urlScan = new Scanner(url);
urlScan.useDelimiter("/");
while(urlScan.hasNext()) {
System.out.println("" + urlScan.next());
System.out.println();
}
}
}
My bastardization attempt at 1.4.2:
import java.io.*;
public class URLDissector {
public static void main (String args[]) {
String url;
try {
FileReader fr = new FileReader("urls.inp");
BufferedReader br = new BufferedReader(fr);
while((url=br.readLine()) != null) {
System.out.println("URL: " + url);
StringReader r = new StringReader(url);
StreamTokenizer inStream = new StreamTokenizer(r);
// unicode 47 is the "/"
inStream.whitespaceChars(47,47);
int tokVal;
while ((tokVal = inStream.nextToken()) != StreamTokenizer.TT_EOF) {
switch (tokVal) {
case StreamTokenizer.TT_WORD:
System.out.println("" + inStream.sval);
break;
case StreamTokenizer.TT_NUMBER:
System.out.println("" + inStream.nval);
break;
}
}
System.out.println();
}
fr.close();
}
catch (Exception exc) {
System.out.println("Error Occurred");
System.exit(1);
}
}
}
I am not trying to handle all URL types or make this an actual production program. I am only trying to make it as good as the first example, which was written using the Scanner class in 1.5. My textbook is showing me how to do things in 1.5, but I am trying to learn how IO for the same exercise is handled in 1.4.2, and any feedback you can give me on the above attempt is appreciated.
Brian Feeny

