Link to home
Start Free TrialLog in
Avatar of NSing
NSing

asked on

How do I retain values entered in a textfield for re-calculation?

How do I retain values entered in a textfield for re-calculation?  I am attempting to create a gas mileage calculator that will take in three values from a user entered in three different textfields.  I have setup 5 invalid response conditions that will display an error message and return focus to the textfield responsible for the invalid response.  I am trying to use the values entered  in the textfields that were correct to recalculate the mileage after the textfield where focus was returned has been edited.  It appears that currently, after focus has been returned and my edits to that textfield has been made, when I try to recalculate, the program does nothing.  It appears to have ended after focus was returned to the invalid textfield.  Also, is their a better way to handle the divide 0 condition?  It appears that the program is still continuing with the calculation with causes the program to end although the invalid response message is displayed.

Below is the code that I have:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class GasMileageCalculator extends MIDlet implements CommandListener{
   
   private Display display;
   private Form form;
   
   private TextField beginMileageField;
   private TextField endMileageField;
   private TextField gallonsField;
   private TextField gasMileageField;
   
   final int COUNT = 4;
   private Item elements[] = new Item[COUNT];
   private String begin, end, gallons;
   private Command quit, calculate;
   
   public GasMileageCalculator()
   {
        display = Display.getDisplay(this);
         
        form = new Form("Singletary Gas Mileage");
           
        beginMileageField = new TextField("Enter Begin Mileage:", "", 20, TextField.NUMERIC);
        endMileageField = new TextField("Enter End Mileage:", "", 20, TextField.NUMERIC);
        gallonsField = new TextField("Enter Gallons:", "", 20, TextField.DECIMAL);
        gasMileageField = new TextField("Gas Mileage:", "", 20, TextField.DECIMAL);
             
        elements[0] = beginMileageField;
        elements[1] = endMileageField;
        elements[2] = gallonsField;
        elements[3] = gasMileageField;
       
        for(int itr = 0; itr < COUNT; itr++){
            form.append(elements[itr]);
        }
   
        quit = new Command("Quit", Command.EXIT, 1);
        calculate = new Command("Calculate", Command.SCREEN, 2);
        form.addCommand(quit);
        form.addCommand(calculate);        
        form.setCommandListener(this);
   }

   protected void startApp()throws MIDletStateChangeException{
      display.setCurrent(form);
   }

   protected void pauseApp(){
   }

   protected void destroyApp(boolean unconditional)throws MIDletStateChangeException{
   }

   public void commandAction(Command command, Displayable displayable)
   {

       double dA, dB, dC;
       double total;
       gasMileageField.setString("");    

            beginMileageField = (TextField)form.get(0);
            begin = beginMileageField.getString();
       
            endMileageField = (TextField)form.get(1);
            end = endMileageField.getString();
     
            gallonsField = (TextField)form.get(2);
            gallons = gallonsField.getString();

            dA = 0;
            dB = 0;
            dC = 0;
            total = 0;
 
            dA = Double.valueOf(begin).doubleValue();
            dB = Double.valueOf(end).doubleValue();
            dC = Double.valueOf(gallons).doubleValue();
           
            if (dB < dA){
                display.setCurrentItem(endMileageField);
                System.out.println ("Invalid response.  End mileage is less than begin mileage.");
            }
           
            else if (dA < 0){
                display.setCurrentItem(beginMileageField);
                System.out.println ("Invalid response.  Begin mileage is less 0.");
            }
           
            else if (dB < 0){
                display.setCurrentItem(endMileageField);
                System.out.println ("Invalid response.  End mileage is less than 0.");
            }
           
            else if (dC < 0){
                display.setCurrentItem(gallonsField);
                System.out.println ("Invalid response.  Gallons is less than 0.");
            }
           
            else if (dC == 0){
                display.setCurrentItem(gallonsField);
                System.out.println ("Invalid response.  Gallons equals 0.");
            }
           
       if (command == calculate){
           total = (dB - dA)/dC;
           
           //Format output to two decimal places
           double formattedTotal = (double)(int)((total)*100.0)/100.0;
           
           gasMileageField.setString(String.valueOf(formattedTotal));
        }
        try
        {
            if (command == quit){
                destroyApp(true);
                notifyDestroyed();
            }
        }
        catch (MIDletStateChangeException me){
        }
   }
}
Avatar of robthewolf
robthewolf
Flag of Israel image

This is a bad idea
else if (dC == 0){
the double dC could have a floating point error, so it will not exactly be 0.  You should truncate it or cast it to and int so that if it is meant to be 0 then the if statement will fire correctly.
Avatar of NSing
NSing

ASKER

I'm not sure if I know how to do what you have advised. Would the cast to int come before the if statement is reached?  Any ideas on retaining the other values in the textfields for recalculation?
you could probably just do this:
else if( ((int)dC) == 0) {

what it does is return the int value of dC to the equality statement.  

I havent had a chance to go through the code to find out what is happening for the textfields data
Avatar of NSing

ASKER

Thanks.  I'm still stuck on retaining the values in the textfields for recalculation.
ASKER CERTIFIED SOLUTION
Avatar of robthewolf
robthewolf
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NSing

ASKER

Thanks it worked!  I also made the change related to the local variables.