|
Newbie
Join Date: Dec 2006
Location: Norway
Posts: 4
Rep Power: 0 
|
Help needed with using TextFields as user input
I'm slowly learning java so I can make midlets for my cell phone. I thought it would be best to start with a simple calculator program, so I could get familiar with java. It was easy enough to make a working version for command line use, but porting it over to a midlet is proving to be more difficult. So far I've put together a form with the input fields required, but I can't extract values from the textfields. I've tried several methods, but my IDE just returns errors. What I need help with is gathering values from the textfields, and displaying values in the textfields. Here's the code so far. Oh btw, it's a chemistry calc.
/* * pHCalculator.java * * Created on November 17, 2007, 7:55 PM */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*; /** * * @author Eirik * @version */ public class pHCalculator extends MIDlet implements javax.microedition.lcdui.CommandListener { public pHCalculator() { } private Form calcForm; private Command exitcommand; private Command helpcommand; private Form help; private Command backfromhelp; private TextField input_ka; private TextField input_m; private TextField input_ph; double ph = 0, K = 0, M = 0; private void initialize() { getDisplay().setCurrent(get_calcForm()); } public void commandAction(Command command, Displayable displayable) { if (displayable == calcForm) { if (command == exitcommand) { exitMIDlet(); } else if (command == helpcommand) { getDisplay().setCurrent(get_help()); } } else if (displayable == help) { if (command == backfromhelp) { getDisplay().setCurrent(get_calcForm()); } } } public Display getDisplay() { return Display.getDisplay(this); } public void exitMIDlet() { getDisplay().setCurrent(null); destroyApp(true); notifyDestroyed(); } public Form get_calcForm() { if (calcForm == null) { calcForm = new Form(null, new Item[] { new StringItem("pH Calculator",""), input_area_ka(), input_area_m(), input_area_ph() }); calcForm.addCommand(get_exitcommand()); calcForm.addCommand(get_helpcommand()); calcForm.setCommandListener(this); } return calcForm; } public TextField input_area_ka() { if (input_ka == null) { input_ka = new TextField("Ka", "0", 120, TextField.NUMERIC); } return input_ka; } public TextField input_area_m() { if (input_m == null) { input_m = new TextField("Acid Concentration (M)", "0", 120, TextField.NUMERIC); } return input_m; } public TextField input_area_ph() { if (input_ph == null) { input_ph = new TextField("pH of Solution", "0", 120, TextField.NUMERIC); } return input_ph; } public Command get_exitcommand() { if (exitcommand == null) { exitcommand = new Command("Exit", Command.EXIT, 1); } return exitcommand; } public Command get_helpcommand() { if (helpcommand == null) { helpcommand = new Command("Help", Command.HELP, 1); } return helpcommand; } public Form get_help() { if (help == null) { help = new Form(null, new Item[] { new StringItem("Help Contents", ""), new StringItem("", "Enter Ka first. Then acid concentration or pH depending on which is known. Mark unknowns with 0.") }); help.addCommand(get_backfromhelp()); help.setCommandListener(this); } return help; } public Command get_backfromhelp() { if (backfromhelp == null) { backfromhelp = new Command("Back", Command.BACK, 1); } return backfromhelp; } public void startApp() { initialize(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
And the code I'm tring to port it from. Just so the previous makes more sense.
/* * Main.java * * Created on November 14, 2007, 5:35 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package phcalc; /** * * @author Eirik */ import java.io.*; public class Main { /** Creates a new instance of Main */ public Main() { } /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { double ph = 0, K = 0, M = 0; System.out.println("Enter Ka first. Then acid concentration or pH depending on which is known. Mark unknowns with 0."); try { InputStreamReader inputStreamReader = new InputStreamReader ( System.in ); BufferedReader in = new BufferedReader ( inputStreamReader ); System.out.println("Enter Ka "); String acidk = in.readLine(); K = Double.parseDouble(acidk); System.out.println("Enter acid concentration "); String acidmolar = in.readLine(); M = Double.parseDouble(acidmolar); System.out.println("Enter pH "); String solutionpH = in.readLine(); ph = Double.parseDouble(solutionpH); } catch(IOException ioex) { System.out.println("Input error"); System.exit(1); } catch(NumberFormatException nfex) { System.out.println("\"" + nfex.getMessage() + "\" is not numeric"); System.exit(1); } if ( M == 0 ) { M = (((Math.pow((K+2*(Math.pow(10,-ph))),2))-K*K)/(4*K)); System.out.println("Concentration: " + M ); System.exit(0); } if ( ph == 0) { ph = (-Math.log10((-K+Math.sqrt((K*K)-4*-K*M))/2)); System.out.println("pH: " + ph ); System.exit(0); } } }
|