开发者

How to make simple dicitonary J2ME

开发者 https://www.devze.com 2023-01-03 02:22 出处:网络
I am beginner in JavaME. I\'d like to make simple dicitionary. The source data is placed on \"data.txt\" file in \"res\" directory. The structure is like this:

I am beginner in JavaME. I'd like to make simple dicitionary. The source data is placed on "data.txt" file in "res" directory. The structure is like this:

#apple=kind of fruit;

#spinach=kind of vegetable;

开发者_如何学编程

The flow is so simple. User enters word that he want to search in a text field, e.g "apple", system take the user input, read the "data.txt", search the matched word in it, take corresponding word, and display it to another textfield/textbox.

I've managed to read whole "data.txt" using this code..

private String readDataText() {
    InputStream is = getClass().getResourceAsStream("data.txt");
    try {
        StringBuffer sb = new StringBuffer();
        int chr, i=0;
        while ((chr = is.read()) != -1)
            sb.append((char) chr);
        return sb.toString();
    }
    catch (Exception e) {
    }
    return null;
}

but I still dont know how to split it, find the matched word with the user input and take corresponding word. Hope somebody willing to share his/her knowledge to help me..


Basically you want something like this:

private String readDataText() {
    InputStream is = getClass().getResourceAsStream("data.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
    try {
        while ((line = br.readLine()) != null)
        {
            String[] split = line.split("=");
            if (split[0].equals("#someFruit"))
                return split[1];
        }
    }
    catch (Exception e) {}
    return null;
}
  1. Read the line using a BufferedReader, no need to handle single chars.
  2. Split the line by the = token
  3. Check if the key in the dictionary is the wanted key, if so, return the value.
0

精彩评论

暂无评论...
验证码 换一张
取 消