diff options
author | RossTheRoss <mstrapp@protonmail.com> | 2020-01-30 16:55:04 -0600 |
---|---|---|
committer | RossTheRoss <mstrapp@protonmail.com> | 2020-01-30 16:55:04 -0600 |
commit | 175721a63b426355274fa9e8063f762020ab8362 (patch) | |
tree | cf2c1b33233d660c9f50de5e659b9343bb264984 /csci1913/Java/project3/Words.java | |
parent | Make Python thing in Python (diff) | |
download | homework-175721a63b426355274fa9e8063f762020ab8362.tar homework-175721a63b426355274fa9e8063f762020ab8362.tar.gz homework-175721a63b426355274fa9e8063f762020ab8362.tar.bz2 homework-175721a63b426355274fa9e8063f762020ab8362.tar.lz homework-175721a63b426355274fa9e8063f762020ab8362.tar.xz homework-175721a63b426355274fa9e8063f762020ab8362.tar.zst homework-175721a63b426355274fa9e8063f762020ab8362.zip |
R E A R R A N G E
Diffstat (limited to 'csci1913/Java/project3/Words.java')
-rw-r--r-- | csci1913/Java/project3/Words.java | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/csci1913/Java/project3/Words.java b/csci1913/Java/project3/Words.java deleted file mode 100644 index 0487267..0000000 --- a/csci1913/Java/project3/Words.java +++ /dev/null @@ -1,90 +0,0 @@ -package project3; -// -// WORDS. An iterator that reads lower case words from a text file. -// -// James Moen -// 19 Apr 17 -// - -import java.io.FileReader; // Read Unicode chars from a file. -import java.io.IOException; // In case there's IO trouble. - -// WORDS. Iterator. Read words, represented as STRINGs, from a text file. Each -// word is the longest possible contiguous series of alphabetic ASCII CHARs. - -class Words { - private int ch; // Last CHAR from READER, as an INT. - private FileReader reader; // Read CHARs from here. - private StringBuilder word; // Last word read from READER. - - // Constructor. Initialize an instance of WORDS, so it reads words from a file - // whose pathname is PATH. Throw an exception if we can't open PATH. - - public Words(String path) { - try { - reader = new FileReader(path); - ch = reader.read(); - } catch (IOException ignore) { - throw new IllegalArgumentException("Cannot open '" + path + "'."); - } - } - - // HAS NEXT. Try to read a WORD from READER, converting it to lower case as we - // go. Test if we were successful. - - public boolean hasNext() { - word = new StringBuilder(); - while (ch > 0 && !isAlphabetic((char) ch)) { - read(); - } - while (ch > 0 && isAlphabetic((char) ch)) { - word.append(toLower((char) ch)); - read(); - } - return word.length() > 0; - } - - // IS ALPHABETIC. Test if CH is an ASCII letter. - - private boolean isAlphabetic(char ch) { - return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z'; - } - - // NEXT. If HAS NEXT is true, then return a WORD read from READER as a STRING. - // Otherwise, return an undefined STRING. - - public String next() { - return word.toString(); - } - - // READ. Read the next CHAR from READER. Set CH to the CHAR, represented as an - // INT. If there are no more CHARs to be read from READER, then set CH to -1. - - private void read() { - try { - ch = reader.read(); - } catch (IOException ignore) { - ch = -1; - } - } - - // TO LOWER. Return the lower case ASCII letter which corresponds to the ASCII - // letter CH. - - private char toLower(char ch) { - if ('a' <= ch && ch <= 'z') { - return ch; - } else { - return (char) (ch - 'A' + 'a'); - } - } - // MAIN. For testing. Open a text file whose pathname is the 0th argument from - // the command line. Read words from the file, and print them one per line. - - public static void main(String[] args) { - Words words = new Words(args[0]); - while (words.hasNext()) { - System.out.println("'" + words.next() + "'"); - } - } -} |