views:

21

answers:

1

Hi, 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..

A: 

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.
Yuval A
If the value contains "=", the result will only contain the first part
mhaller
We cant use BufferedReader in J2ME, can we?
batosai_fk
http://stackoverflow.com/questions/200239/how-do-i-read-strings-in-j2me
Yuval A
sorry, but still unclear for me. Would u mind to give direct explanation..?
batosai_fk
Read the answers - they are more than helpful.
Yuval A