Jump to content

Java ME assignment errors

- - - - -

  • Please log in to reply
No replies to this topic

#1
OMGNinja

OMGNinja

    Newbie

  • Members
  • Pip
  • 3 posts
Ok so I am trying to calculate gas milage by using (ending miles - begning miles) / gas used. This works fine and get the result I want, but when I am trying to display that results to the screen using the form.append(endResult); it either kills the app or it set the end result to 0 depending on where I place the endResult variable. I relly dont know what I am doing wrong here.
here is the code thanks for any help

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.lcdui.TextField;




public class Gas extends MIDlet  implements CommandListener {


  

    //gas mileage = (em - bm)/ gas;


TextField gas; //gas in gallons

TextField bm;  //begingin mileage

TextField em;  //ending mileage

private int result; // //gas mileage = (em - bm)/ gas;

private String endResult; //for print the result

private int gInput;

private int bMInput;

private int eMInput;




    private Form form;

    private Command quit;

    private Command OKCmd;



public Gas(){

    gas = new TextField("Gas in Gallons:", "", 10, TextField.ANY);

    bm = new TextField("Begining Mileage :", "", 10, TextField.ANY);

    em = new TextField("Ending Mileage :", "", 10, TextField.ANY);

    


      form = new Form("Gas Milages");

     

      form.setCommandListener(this);

      quit = new Command("Quit", Command.SCREEN, 1);

      form.addCommand(quit);

      OKCmd = new Command("OK", Command.SCREEN, 1);

      form.addCommand(OKCmd);

      form.append(gas);

      form.append(bm);

      form.append(em);

      

      

      



   }

protected void startApp() throws MIDletStateChangeException{


      Display.getDisplay(this).setCurrent(form);

   }



   protected void pauseApp(){

   }



   protected void destroyApp(boolean unconditional)

           throws MIDletStateChangeException{

   }



   // Handle Events generated by Commands

   public void commandAction(Command command, Displayable displayable){

try{

       if (command == OKCmd){

       gInput = Integer.parseInt(gas.getString());

       bMInput = Integer.parseInt(bm.getString());

       eMInput = Integer.parseInt(em.getString());

       result = (eMInput -bMInput) / gInput ;

     

       }

}catch(NumberFormatException e){}

        endResult = new String("The Amount of Gas used is " + result);

        form.append(endResult);

       

       try

      {

         if (command == quit)

         {

            destroyApp(true);

            notifyDestroyed();

         }

      }


      catch (MIDletStateChangeException me){

      }

   }

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users