Hi there, I'm new here, and have just started using J2ME to further my understanding of programming (Even though I have been studying Java for the best part of an academic year now), but have got a little bit stuck writing my first small application (After Hello World of course!), which is why I need some guidance from some far more knowledgable folk!
I have done quite a bit of searching, and scrolling through tutorials and API's but I'm not sure what I'm doing wrong, so I thought it'd be okay to ask here.
Basically I've tried to write a Paypal fee calculator, just a really simple application which takes a value from a text box, and when the user hits okay, it performs a small calculation, and then shows what the final amount will be. I know such applications exist (And far more complex than my own), but I wanted to start out simple. I have also tried to include error handling where if a non number is entered it switches to a error message.
This however is where my problem arises, I'd like to implement a back command from both errorForm and resultForm (See Code) but everything I have tried does not seem to work. I'm not sure if becuase what I have written in the listener is wrong, or I have badly implemented something elsewhere which is causing the problem.
Also, if there's any other obvious problems or redundencies in my code I'd quite like to know, so I can understand what I'm doing wrong in the future.
I also have a few other questions which I haven't found a straight answer for.
1) When using commands, such as ok = new Command("OK", Command.OK, 2); What does the integer refer too? I'm not quite sure how changing this number affects the running of the program. When I played around with it, it didn't seem to have an obvious effect.
2)Is it necessary to use display = Display.getDisplay(this); at the begginning of methids like I have done? I understand why we do it during startApp(); but the program runs fine with the ones I have commented out.
Also, I'd rather not just be given code to copy, If possible, an explanation and a sample will do fine, after all I'm trying to make myself a better programmer, not just plagarising someone's code.
Also, apologies if the questions seem really simple, I have sieved through alot of tutorials, but I can't seem to get it to work. And also Moderators if this is in the wrong place, feel free to move it.
Many Thanks,
Michael.
Code:package com.example.michaelapp; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class MichaelAppMidlet extends MIDlet implements CommandListener{ private Form mainForm, resultForm, errorForm; private Display display; private TextField pounds; private Command ok, exit, back; //////////////////////////////////////////////////////// public MichaelAppMidlet(){ pounds = new TextField("(£) Pounds: ", "0.00", 10, TextField.ANY); exit = new Command("Exit", Command.EXIT, 1); ok = new Command("OK", Command.OK, 2); back = new Command("Back", Command.BACK, 1); } //////////////////////////////////////////////////////// public void startApp(){ display = Display.getDisplay(this); Form mainForm = new Form("PayPal Calculator"); mainForm.append(pounds); mainForm.addCommand(ok); mainForm.addCommand(exit); mainForm.setCommandListener(this); display.setCurrent(mainForm); } public void pauseApp(){ } public void destroyApp(boolean destroy){ notifyDestroyed(); } //////////////////////////////////////////////////////// public void showInput(){ //display = Display.getDisplay(this); String input = pounds.getString(); try{ double num = Double.valueOf(input.trim()).doubleValue(); //Parse Input double midVal = (num*0.034); double longVal = (num-midVal)- 0.20; int x = (int)(longVal * 100.0); double finalVal = ((double)x)/100.0; Form resultForm = new Form("Your Fees"); resultForm.append("You final amount after fees is £" + finalVal + ". "); resultForm.addCommand(back); resultForm.setCommandListener(this); display.setCurrent(resultForm); }catch(NumberFormatException e){ error(); } } //////////////////////////////////////////////////////// public void error(){ //display = Display.getDisplay(this); Form errorForm = new Form("Error"); errorForm.append("You have entered an invalid number. Try making sure " + "you are inputing a number greater than zero, or perhaps you have " + "accidentally entered a letter"); errorForm.addCommand(back); errorForm.setCommandListener(this); display.setCurrent(errorForm); } //////////////////////////////////////////////////////// public void commandAction(Command c, Displayable d) { if(c == ok){ showInput(); } else if (c == back){ display.setCurrent(mainForm); } else if (c == exit){ destroyApp(false); } } }

Reply With Quote

