Comparable and Serializable interfaces

Hi All,
I have developed a program in java which implements Comparable and Serializable Interfaces. After, I decided to run the program in J2ME. But later on, I found that MIDP doesn't implement these interfaces.
Some one can help me how to implement my own interfaces.
Thank you

Hi Supareno
I need urgently your help.
I developed Java program which works as predicitive text system for my mother tongue.I would like to move from Java to J2ME. But until now I have problems. please can u help me. the following code run in command line.
I tried to develop my own interfaces as you told me but it doesn't work.
Help me also for reading and writing text files.
Thank you
import java.util.*;
import java.io.*;
import java.util.Vector;
import java.util.Enumeration;
public class PredText {
private Vector dict;
public static final String dictionaryFile = "C:/java/words.txt";
// convert a string to the corresponding signature
public static String toNumeric (String word) {
String lowerWord = word.toLowerCase();
StringBuffer result = new StringBuffer("");
for (int i = 0; i < lowerWord.length(); i++) {
char c = lowerWord.charAt(i);
if ("abc".indexOf(c) > -1) result.append("2");
else if ("def".indexOf(c) > -1) result.append("3");
else if ("ghi".indexOf(c) > -1) result.append("4");
else if ("jkl".indexOf(c) > -1) result.append("5");
else if ("mno".indexOf(c) > -1) result.append("6");
else if ("pqrs".indexOf(c) > -1) result.append("7");
else if ("tuv".indexOf(c) > -1) result.append("8");
else if ("wxyz".indexOf(c) > -1) result.append("9");
else if (" ".indexOf(c) > -1) result.append("9");
else result.append("?");
return result.toString();
// find all the words corresponding to a signature
public Vector findMatches(String sig) {
WordSig ws = new WordSig(sig, "");
WordSig newws;
Vector results = new Vector();
int index;
index = Collections.binarySearch(dict, ws);
if (index < 0){
// no matches! :(
// try to get the string that starts with a substring like ours.
index=-1-index;
if (((WordSig)dict.get(index)).getSig().startsWith(sig)) {
// no word found. we return those starting with the
// same signature
newws = (WordSig) dict.get(index);
results.addElement(newws.getWord().substring(0,sig.length()));
return results;
} else{
//no match
return results;
} else {
// go back to the first match
while(((WordSig)dict.get(index)).getSig().equals(sig) && index>0)
index--;
if (index != 0)
index++;
while ((newws = (WordSig) dict.get(index)).equals(ws)){
results.addElement( newws.getWord() );
index++;
return results;
// intialises the dictionary
public PredText () {
     String word;
String sig;
Vector dict = null;
// first try to load dict.dat
try {
System.out.print("Trying to load dict.dat...");
FileInputStream fileStream = new FileInputStream("C:/java/dict.dat");
ObjectInputStream objectStream = new ObjectInputStream(fileStream);
dict = (Vector) objectStream.readObject();
fileStream.close();
System.out.println(" done.");
}catch (ClassNotFoundException classNotFoundException) {
     System.out.println("Unable to create an object");     
catch (IOException e) {
// exception: create the dictionary
System.out.println("Error. I'm going to recreate the dictionary file");
try {
dict = createDict();
catch (IOException ioe) {
System.err.println("Error while creating dictionary file. Exiting." + e);
System.exit(1);
this.dict = dict;
// create the dictionary serialised file
public Vector createDict () throws IOException {
String word;
Vector dict = new Vector();
//open the dictionary file
System.out.print("Reading the dictionary... ");
BufferedReader dictFile = new BufferedReader(
new FileReader(dictionaryFile));
//insert each word into the data structure
while ((word = dictFile.readLine()) != null) {
word = word.toLowerCase();
dict.addElement(new WordSig(toNumeric(word), word));
// List list = dict.subList(0, dict.size());
List list = dict.subList(0, dict.size());
Collections.sort(list);
System.out.println("done.");
System.out.print("Writing the dictionary... ");
FileOutputStream fileStream = new FileOutputStream("C:/java/dict.dat");
ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
objectStream.writeObject(dict);
objectStream.flush();
fileStream.close();
System.out.println("done.");
return dict;
public static void main (String args[]) {
     PredText pt = new PredText();
if (args.length == 0) {
// no arguments, find the largest clash
     Vector result;
     Enumeration e = pt.dict.elements();
String currentSig = "";
String prevSig = "";
int clash = 0;
int maxClash = 0;
String clashSig = "";
while (e.hasMoreElements()) {
WordSig ws = (WordSig) e.nextElement();
currentSig = ws.getSig();
if (currentSig.equals(prevSig)) {
// another word with the same signature
clash ++;
} else {
// new signature: check if the previous one led
// to a maximal clash
if (clash > maxClash) {
clashSig = prevSig;
maxClash = clash;
clash = 1;
prevSig = currentSig;
result = pt.findMatches(clashSig);
System.out.println("The signature leading to most clashes is "
+ clashSig + " with " + result.size() +
" number of clashes");
for (int j = 0; j < result.size(); j++) {
System.out.println((String)result.get(j));
} else if ("0123456789".indexOf(args[0].charAt(0)) > -1){
// numeric input: find the matches for the argument
Vector result;
result = pt.findMatches(args[0]);
for (int j = 0; j < result.size(); j++) {
System.out.println((String)result.get(j));
} else {
// convert each word to the corresponding signature
for (int i = 0; i < args.length; i++) {
System.out.print(toNumeric(args) + " ");
class WordSig implements Comparable, Serializable {
String word;
String sig;
public WordSig (String sig, String word) {
this.sig = sig;
this.word = word;
public String getSig () {
return this.sig;
public String getWord() {
return this.word;
public int compareTo (Object ws) {
return sig.compareTo(((WordSig) ws).getSig());
public boolean equals (Object ws) {
return sig.equals(((WordSig) ws).getSig());
public String toString () {
return sig + ", " + word;

Similar Messages

  • Comparable and Serializable Interfaces in MIDP

    Hi All,
    I have developed a program in java which implements Comparable and Serializable Interfaces. After, I decided to run the program in J2ME. But later on, I found that MIDP doesn't implement these interfaces.
    Some one can help me how to implement my own interfaces.
    Thank you

    Thank you for replying to me.
    My problem is to compare to objects in MIDP.
    You know that in java is possible to implements directly the Comparable interface but in MIDP is not the same. So I think it requires to create your own. Is that my question.
    I need some to help me. just to avoid the implentation of comparable interface from the java lang package.
    Thank u.

  • Comparable, cloneable, serializable interface

    Dear all,
    Is there any example and explaination on the usage of comparable, cloneable, serializable interfaces besides the official documentation of Sun's Java API?
    Regards.

    Yes, there's Sun's official tutorial too!
    http://java.sun.com/docs/books/tutorial/
    Use the search on the right (not top right, just below that).
    Cheers,
    Radish21

  • Restart persistance and Serializable interface

    When restarting Tomcat 5.5, I see the following things appearing in my log files:
    WARNING: Cannot serialize session attribute parameters for session 701E5E884E3423FE160B65BF6C44DEEB
    java.io.NotSerializableException: toets.toetselementen.VraagTypeSo i guess this means that the session of certain users of my website get broken. I would like to keep my users satisfied by not breaking their sessions with Tomcat restarts.
    On the Tomcat website, i found the following information:
    [http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html|http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html]
    The bottom of that page mentions that the session attributes must implement the Serializable interface. So I assume VraagType is somewhere used as a session attribute, and it indeed does not implement the Serializable interface:
    package toets.toetselementen;
    public class VraagType {
        private int id;
        private String type;
        public VraagType(int id, String type) {
            this.id = id;
            this.type = type;
        public int id() {
            return id;
        public String type() {
            return type;
    }So I assume the VraagType class must implement the Serializable interface to be able to have (Tomcat 5.5) restart persistance. My question is however: is it enough to simply add 'implements Serializable' to my class definition, or will I also have to implement certain methods?

    Yep, it is basically just a marker interface to let Java know that this class is allowed to be serialized. You don't want to do this with sensitive information such as an User object with a password as unencrypted string property --in such case, you could also declare it transient and live with the fact that it doesn't come back after deserialization.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Comparable and comparator interface in java

    Hi All,
    How comparable and comparator interface works in java and when to use comparable and when to use comparator.please give me some example(code) as I am not able to understand the difference.
    Thanks
    Sumit
    Edited by: sumit7nov on May 17, 2009 4:45 AM

    Thanks,one more doubt.
    We have Collections.sort() method and we can sort any list by this method without implementing comparable or comparator interface.Then my question is when we need to implement comparable or comparator interface or when do we write our own compareTo() or compare() methods.
    Thanks
    Sumit

  • Comparable and Comparator Interface - Collections

    Hi All
    Please let me know when to use Comparable interface and Comparator interface. ?
    Any sort of information is highly appreciable .
    Matt

    Matt wrote:
    @ jverd      
    So, when you googled, and found tutorials, and worked through those tutorials, and read the javadocs for those classes, what in particular did you not understand?If everything would work like that what will be the use of forums like this , It does work like that, for people who aren't lazy. The use of these forums, as indicated by my first reply, is among other things, when you've done your own research, as you should do, and run into something you don't understand.
    so dont try to be smart :(Maybe you should try to be smarter.
    Lets try not to abuse the forum with exchanges like this. The only abuse so far has been you expecting the forum to be a substitute for doing your own research.
    I know how to use Comparable and Comparator but dont know when to use these in a real time application.That statement says nothing about your problem. The only information it carries is that you don't know what the term "real time" means.

  • Web Application and Serialization

    Hi, i have a problem with a web application that is running on an Application server (BEA WebLogic 7.0). I have only the war file of the web application and i must know if in this war there is a class that is not serializable.
    How can i find it ? Is there a tool that can help me to find not serializable classes ?
    Thanks.
    claudiozx2006

    One way to do that is to decompile Java classes with Java decompiler and see which one doesn't implement Serializable interface. Here is a link to a good decompiler: http://www.kpdus.com/jad.html

  • Remote and local interfaces....

    Hey.. I used to work with weblogic 8, and now have to work with 10.01....
    I remember reading some documentation about that the container can see what JVM it is running in... and hence that creating localhome interfaces it not necessary.... creating remote interfaces should be ok... as the container wont be making any remote calls and serialization when it can see that the applications are running in the same JVM...
    But where can i find some documentation about this... ???
    Regards
    Kris

    Hi Kristian,
    The term local can be misleading ...
    You've got to understand that when you talk about local with EJBs, it's not in the same JVM but within the same EAR.
    If you want to go further, I detailed the use of EJB3 on WLS 10 on my blog :
    http://m-button.blogspot.com/2008/07/reminder-on-how-to-use-ejb3-with.html
    Regards,
    Maxence.

  • What is the use of Serializable Interface in java?

    Hello friends,
    I have one dout and i want to share with u guys:
    Why Serializable interface made and actully
    what's the use of it?
    At which place it become useful ?
    It is not contain any method then why it is made?
    If anyone have any idea about this then please reply.
    Thanks in advace
    Regards,
    Jitendra Parekh

    t is not contain any method then why it is made?To point out to the user of a class (and the programs) that the design of this class is conforming to certain restraints needed for Serialization.

  • Using Serializable interface

    Hi, I have to write a program that implements the Serializable interface along with a LinkedList. It looks like this:
    public class Library implements Serializable{
       private LinkedList<LibraryBranch> collection;
    }However, all of my methods after that give a warning/error "class, interface, or enum expected." Do I have to write my own methods for Serializable that allows me to read and write data? I don't completely understand the Serializable interface description in the API.
    Thanks for your help.
    - Jeremy

    I don't know, the assignment asks for it. I think I have to be able to write the objects into a file and read them back again later.

  • Why do we need Serializable Interface since there is no method decl. inside

    On working with Serialization in java
    I could see that the Serializable Interface is empty without any method inside.
    Also, the writeObject() and readObject() accepts only if the Serializable interface is implemented to a class.
    What is the use of implementing an empty Interface?
    Please clarify.
    Thanks in advance.
    Regards,
    R.Mahendra Babu

    The completely empty Serializable is only a marker interface -- it simply allows the serialization mechanism to verify that the class is able to be persisted.

  • Whats the difference between comparable and comparator?

    whats the difference between comparable and comparator?
    when must i use comparable, and when must i use comparator?

    whats the difference between comparable and
    comparator?Comparable is from the java.lang package, Comparator from java.util.
    when must i use comparable, and when must i use
    comparator?Here's a tutorial on both:
    http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

  • Drag and Drop questions and Serialization in the 1Z0-852 Upgrade exam?

    Hi,
    Does anyone know if the drag and drop items and serialization have been removed from the 1Z0-852 Java SE 6 Programmer Certified Professional UPGRADE exam?
    I know that they have been removed from the 1Z0-851, but not sure about the upgrade exam.
    Thanks in advance,
    Alvaro

    Alvaro wrote:
    Hi,
    Does anyone know if the drag and drop items and serialization have been removed from the 1Z0-852 Java SE 6 Programmer Certified Professional UPGRADE exam?
    I know that they have been removed from the 1Z0-851, but not sure about the upgrade exam.
    Thanks in advance,
    Alvaro(1) Drag and Drop
    .... this question format will have been removed:
    Ref: http://blogs.oracle.com/certification/entry/0596
    (2) Serialization:
    It is best to compare the topics from your notes to the current topics, however according to my reading of the current topics it is in scope at least to some extent:
    http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=41&p_exam_id=1Z0_852 (Section 3 API Contents):
    Develop code that serializes and/or de-serializes objects using the following APIs from java.io: DataInputStream, DataOutputStream, FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream and Serializable.

  • Version ID and Serializable problem...

    Ok the problem is this. I have a class for some items that will be saved to disk using the Serializable interface. Now to be able to load old files after I have edited the class I need to include the version id in the class like:
    static final long serialVersionUID = 1407381982932730981L;The thing is, to get that version ID you need to compile the class and go into a commandline and run the serialver tool on the class.
    Now, i will have a lot of new files that will extend the main class. And these will also be saved down to files (and it is critical that old files can be deserialized when the program starts again). So for each new class, i will have to go and get the version id.
    Is there some easier way for this?

    It's not entirely clear to me what your problem is. You talk about new classes that extend the main class. Serialization version issues cannot be a problem there.
    However, it is possible to run the serialver program directly from another class by invoking its main method. It's the class sun.tools.serialver.SerialVer. You'll need to include the tools. jar file in your class path. By capturing its output (using System.setOut()) you can automate this process to whatever degree you feel is worthwhile.
    Sylvia.

  • Example class that implements Serializable interface

    Dear,
    I have a class myData that I want to implement Serializable interface. class myData has only two fields: Integer iData1, String sData2.
    Could anybody shown me how my myData class should be?
    Thanks a lot!

    Hey, if you have yet to obtain a remote reference from the app server ...then we are into pandora's box. I lost three whole heads of hair getting up on JBoss when I first started. You want to check out the JBoss forums on the JBoss website, and the enterprise javabeans forum here. Search some posts and read the free JBoss manual.
    Unfortunately, there isn't a 'here, do this' solution to getting connected with JBoss. There are quite a few gotcha's. There are descriptors, descriptor syntax ...and this changes between releases so there seems to be alot of people saying 'here, do this' ...but you try and it doesn't work (wrong release). Here are some descriptors that I threw up recently for someone ...a place to start.
    http://forum.java.sun.com/thread.jsp?forum=13&thread=414432
    This drove me nuts until it all worked right. I was stuck for three weeks at one point ...ready to give up, but then I got it. Perservere ...its a nice container for learning in (its free!).
    I will try and watch for you.
    Oh, and put something in your head ...at least then you will keep your hair !
    :)

Maybe you are looking for

  • Will the 20" work with a Mac Mini?

    I've heard that the apple cinema monitors only work on PowerMac G4's and G5's. Is a Mac Mini a powermac g4. or will the monitor work with a mac mini? mike

  • Group Messaging - Can it be turned off?

    Hi - I've been searching the net and have called to ask this question and am now posting this question as a last resort to try to resolve this.  Is there any way to turn off group messaging so that when you reply to a pic message / joke for example y

  • Parallel process in process chains

    Hi All, I have created a process chain with two parallel jobs (for forecast run) in APO DP. whenever i'm checking it, it is showing me that <b>"Too many parallel processes for chosen server"</b> In the diagnosis, the message is "On the server  you ha

  • Changing the nextval of a sequence using alter sequence

    Hello experts, Is there any option to modify the nextval of a sequence using alter sequece command? Thanks in advance. J.Prakash

  • Non cumulative key figure and degenereated dimension?

    Hi experts, can some explain with example what do you mean by non cumulative key fgure? what is the advantage of this key figure? what is denereated dimension and under what scenarios do we go for degenerated  dimension? will be very helpful if you c