Results 1 to 6 of 6
  1. #1
    batosai_fk is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default How to make simple dicitonary J2ME

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    The above code reads all the bytes/chars in the file (including the newlines) and builds a String. You'll need to split that into individual lines and then split each line into a key/value pair. The String class has methods for doing this. Also the StringTokenizer class can be used. Then store the key/value pairs in a Map.
    When the user enters a word, that can be used as key to get the value from the Map.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by batosai_fk View Post
    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.
    Can you get rid of those #s in your file? If so you can a Properties object read its content from the file where the fruits/vegetables are the keys and the descriptions are the associated values; that removes all the burden of splitting and storing the stuff.

    kind regards,

    Jos

  4. #4
    batosai_fk is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    @ Norm : Would u mind to give me some example code about ur suggestion..cause I'm still very new in Java.
    @ Josah : I can rid the #, but I think I can't use key and description method, cause it's not about fruit and vegetable, later it could be:
    merlin=witch from britain
    shinkansen=fastest train from japan
    dll

    I'll be grateful for any help from all of you..

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by batosai_fk View Post
    @ Josah : I can rid the #, but I think I can't use key and description method, cause it's not about fruit and vegetable, later it could be:
    merlin=witch from britain
    shinkansen=fastest train from japan
    Of course those can be read by the Properties object as well; every line should be of the form "key = value"; as long as that applies you can have the Properties object do the reading and the searching. Read its API documentation for a complete description.

    kind regards,

    Jos

  6. #6
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    To Split ur all the file,
    U have to put a Matching character in the file.
    then u hyave to match that character and find the index of that charachter and u can split that file. and u have to create a new file to put ing the splitting String. For Seperation.

Similar Threads

  1. make simple web service
    By BigBear in forum Java Servlet
    Replies: 2
    Last Post: 06-13-2010, 12:53 PM
  2. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  3. Replies: 0
    Last Post: 04-22-2009, 01:30 PM
  4. J2me
    By Renjini in forum CLDC and MIDP
    Replies: 1
    Last Post: 08-14-2008, 10:58 AM
  5. Replies: 2
    Last Post: 05-04-2008, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •