spellcheck.java

来自「Java写的部分数据结构源码」· Java 代码 · 共 75 行

JAVA
75
字号
import java.io.*;class SpellCheck {    static int lineNum = 1;    static String s;    static int ch;    static void readWord(InputStream fIn) {        try {            while (true)                if (ch > -1 && !Character.isLetter((char)ch)) { // skip                      ch = fIn.read();                  // non-letters;                     if (ch == '\n')                          lineNum++;                }                else break;            if (ch == -1)                return;            s = "";            while (ch > -1 && Character.isLetter((char)ch)) {                s += Character.toUpperCase((char)ch);                ch = fIn.read();            }        } catch (IOException io) {            System.out.println("Problem with input.");        }    }    static public void main(String args[]) {        String fileName = "";        InputStream fIn, dictionary;        InputStreamReader isr = new InputStreamReader(System.in);        BufferedReader buffer = new BufferedReader(isr);        Trie trie = null;        try {            dictionary = new FileInputStream("dictionary");            readWord(dictionary);            trie = new Trie(s.toUpperCase());  // initialize root;            while (ch > -1) {                readWord(dictionary);                if (ch == -1)                     break;                trie.insert(s);            }            dictionary.close();        } catch(IOException io) {            System.err.println("Cannot open dictionary");        }        System.out.println("\nTrie sideview: ");        trie.sideView();        ch = ' ';        lineNum = 1;        try {            if (args.length == 0) {                 System.out.print("Enter a file name: ");                 fileName = buffer.readLine();                 fIn = new FileInputStream(fileName);            }            else {                 fIn = new FileInputStream(args[0]);                 fileName = args[0];            }            System.out.println("Misspelled words:");            while (true) {                 readWord(fIn);                 if (ch == -1)                     break;                 if (!trie.found(s))                     System.out.println(s + " on line " + lineNum);            }            fIn.close();        } catch(IOException io) {            System.err.println("Cannot open " + fileName);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?