Question

i'm trying to create an app for J2ME that accepts the users input and PUSHes its characters into STACK then pop it again then check for condition if the character is equal to (don't know char) then in form display the image with a SIGN LANGUAGE.

for short an app that translate inputted string into sign language.

but my problem is when i run my program.

it always display the last character...

can you help me with this....

by the way. heres the code with timer and timer task...

private void startTimer()
{
    TimerTask task = new TimerTask()
    {
        public void run()
        {
        while(!s.empty())
        {
            char hold;
            hold=s.pop();
             for(int ii=0; ii<chars.length; ii++)
            {
                weww=(int)(hold);
                //System.out.println(hold);
                if(weww==chars[ii])
                {
                    ffm.deleteAll();
                    ffm.append(img[ii]);
                }

            }
        }

        }
    };
    timer = new Timer();
    timer.scheduleAtFixedRate(task,1000,5000);           
}

and heres the full code...

public class Midlet extends MIDlet implements CommandListener{
Display display;
Form fm,ffm;
TextField tf;
Command ok,exit;
StringItem wew;
Image img[];
char cch;
String nia;
Stack s;
Ticker a;
StringItem rep;
Timer timer;
TimerTask timertask;
//private char konia;
char[] chars = {'a', 'b', 'c', 'd', 'e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int weww;
public Midlet()
{
    fm=new Form("Name-to-Sign Language");
    exit = new Command("EXIT", Command.EXIT, 1);
    ok= new Command("OK", Command.STOP, 2);
    fm.addCommand(exit);
    fm.addCommand(ok);
    fm.setCommandListener(this);

    wew=new StringItem(null," ");
    wew.setLayout(StringItem.LAYOUT_CENTER);
    fm.append(wew);

    tf=new TextField("Name:","",20,TextField.SENSITIVE);
    tf.setLayout(TextField.LAYOUT_CENTER);
    fm.append(tf);

    ffm=new Form("Translation");
    ffm.addCommand(ok);
    ffm.setCommandListener(this);
    try{
    img = new Image[chars.length];
    try {
        for(int i =0; i<chars.length; i++){
            img[i] = Image.createImage("/picture/" + chars[i] + ".JPG");
        }
    }

    catch(Exception error){
        error.printStackTrace();
    }
    }catch(Exception e){}
}
public void startApp() {
    if(display==null){
        display=Display.getDisplay(this);
    }
    display.setCurrent(fm);
}

public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
    if(c==ok&&d==fm)
    {
        nia=tf.getString();
        int ko=nia.length();
        s = new Stack(ko);

        for (int i =nia.length()-1; i >=0; i--)
        {
             cch = nia.charAt(i);
             s.push(cch);
        }
        a=new Ticker(nia+"-to-Sign Language.");
        ffm.setTicker(a);
        startTimer();
        display.setCurrent(ffm);
    }
    else if(c==ok&&d==ffm)
    {
        tf.delete(0, nia.length());
        timer.cancel();
        ffm.deleteAll();
        display.setCurrent(fm);
    }
    else if(c==exit&&d==fm)
    {
        this.notifyDestroyed();
    }
}
Was it helpful?

Solution

Your problem is that each time you execute run, you're popping all the characters off the stack, and displaying their sign-language equivalents, without any delay at all. That means you reach the last character almost immediately.

I think you should remove the while loop from the run method (maybe change while to if), so that each time the timer runs, there's only one letter displayed.

By the way, there are very many other simplifications that you could make to this code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top