home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.pdx.edu
/
2014.02.ftp.ee.pdx.edu.tar
/
ftp.ee.pdx.edu
/
oss
/
cvs-2004
/
OpenHamLog
/
Attic
/
OpenHamLog.java,v
< prev
next >
Wrap
Text File
|
2003-07-14
|
4KB
|
209 lines
head 1.4;
access;
symbols;
locks; strict;
comment @# @;
1.4
date 2003.07.14.19.37.57; author htodd; state dead;
branches;
next 1.3;
1.3
date 2003.07.09.06.21.09; author htodd; state Exp;
branches;
next 1.2;
1.2
date 2003.07.07.00.57.47; author htodd; state Exp;
branches;
next 1.1;
1.1
date 2003.07.06.03.49.19; author htodd; state Exp;
branches;
next ;
desc
@@
1.4
log
@Moving files and starting ant.
@
text
@//: OpenHamLog.java
// Counts words in a file, outputs
// results in sorted form.
import java.io.*;
import java.util.*;
public class OpenHamLog {
private FileInputStream file;
private StreamTokenizer st;
private Hashtable counts = new Hashtable();
OpenHamLog(String filename)
throws FileNotFoundException {
try {
file = new FileInputStream(filename);
Reader r = new BufferedReader(new InputStreamReader(file));
st = new StreamTokenizer(r);
st.ordinaryChar('.');
st.ordinaryChar('-');
st.wordChars('_','_');
} catch(FileNotFoundException e) {
System.out.println(
"Could not open " + filename);
throw e;
}
}
void cleanup() {
try {
file.close();
} catch(IOException e) {
System.out.println(
"file.close() unsuccessful");
}
}
void countWords() {
try {
while(st.nextToken() !=
StreamTokenizer.TT_EOF) {
String s;
switch(st.ttype) {
case StreamTokenizer.TT_EOL:
s = new String("EOL");
break;
case StreamTokenizer.TT_NUMBER:
s = Double.toString(st.nval);
break;
case StreamTokenizer.TT_WORD:
s = st.sval; // Already a String
break;
default: // single character in ttype
s = String.valueOf((char)st.ttype);
}
if(counts.containsKey(s))
((Counter)counts.get(s)).increment();
else
counts.put(s, new Counter());
}
} catch(IOException e) {
System.out.println(
"st.nextToken() unsuccessful");
}
}
void printWords() {
try {
while(st.nextToken() !=
StreamTokenizer.TT_EOF) {
String s;
switch(st.ttype) {
case StreamTokenizer.TT_EOL:
s = new String("EOL");
break;
case StreamTokenizer.TT_NUMBER:
s = Double.toString(st.nval);
break;
case StreamTokenizer.TT_WORD:
s = st.sval; // Already a String
break;
default: // single character in ttype
s = String.valueOf((char)st.ttype);
}
System.out.println(s);
}
} catch(IOException e) {
System.out.println(
"st.nextToken() unsuccessful");
}
}
Enumeration values() {
return counts.elements();
}
Enumeration keys() { return counts.keys(); }
Counter getCounter(String s) {
return (Counter)counts.get(s);
}
Enumeration sortedKeys() {
Enumeration e = counts.keys();
StrSortVector sv = new StrSortVector();
while(e.hasMoreElements())
sv.addElement((String)e.nextElement());
// This call forces a sort:
return sv.elements();
}
public static void main(String[] args) {
try {
OpenHamLog wc = new OpenHamLog(args[0]);
wc.printWords();
// wc.countWords();
// Enumeration keys = wc.sortedKeys();
// while(keys.hasMoreElements()) {
// String key = (String)keys.nextElement();
// System.out.println(key + ": "
// + wc.getCounter(key).read());
// }
wc.cleanup();
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
@
1.3
log
@*** empty log message ***
@
text
@@
1.2
log
@Sunday night
@
text
@a6 6
class Counter {
private int i = 1;
int read() { return i; }
void increment() { i++; }
}
@
1.1
log
@Just adding a garbage file to start cvs.
@
text
@d27 1
d42 2
a43 1
void countWords() {
d71 27
d117 8
a124 7
wc.countWords();
Enumeration keys = wc.sortedKeys();
while(keys.hasMoreElements()) {
String key = (String)keys.nextElement();
System.out.println(key + ": "
+ wc.getCounter(key).read());
}
@