如何制作简单的字典J2ME

发布于 2024-09-05 02:29:07 字数 670 浏览 1 评论 0原文

我是 JavaME 初学者。我想做一个简单的字典。源数据放置在“res”目录中的“data.txt”文件中。结构是这样的:

#apple=水果种类;
#菠菜=一种蔬菜;

流程就这么简单。用户在文本字段中输入要搜索的单词,例如“apple”,系统接受用户输入,读取“data.txt”,在其中搜索匹配的单词,取出相应的单词,并将其显示到另一个文本字段/文本框。

我已经设法使用此代码读取整个“data.txt”..

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;
}

但我仍然不知道如何拆分它,找到与用户输入匹配的单词并获取相应的单词。希望有人愿意分享他/她的知识来帮助我..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-09-12 02:29:07

基本上你想要这样的东西:

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. 使用 BufferedReader 读取行,不需要处理单个 char 。
  2. = 标记分割行
  3. 检查字典中的键是否是所需的键,如果是,则返回值。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文