View Single Post
Old Nov 18th, 2007, 5:48 AM   #1
uzzors2k
Newbie
 
Join Date: Dec 2006
Location: Norway
Posts: 4
Rep Power: 0 uzzors2k is on a distinguished road
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.

java Syntax (Toggle Plain Text)
  1. /*
  2.  * pHCalculator.java
  3.  *
  4.  * Created on November 17, 2007, 7:55 PM
  5.  */
  6.  
  7. import javax.microedition.midlet.*;
  8. import javax.microedition.lcdui.*;
  9. import java.io.*;
  10.  
  11. /**
  12.  *
  13.  * @author Eirik
  14.  * @version
  15.  */
  16. public class pHCalculator extends MIDlet implements javax.microedition.lcdui.CommandListener
  17. {
  18.  
  19. public pHCalculator() {
  20. }
  21. private Form calcForm;
  22. private Command exitcommand;
  23. private Command helpcommand;
  24. private Form help;
  25. private Command backfromhelp;
  26. private TextField input_ka;
  27. private TextField input_m;
  28. private TextField input_ph;
  29. double ph = 0, K = 0, M = 0;
  30.  
  31. private void initialize() {
  32. getDisplay().setCurrent(get_calcForm());
  33. }
  34.  
  35. public void commandAction(Command command, Displayable displayable) {
  36. if (displayable == calcForm) {
  37. if (command == exitcommand) {
  38. exitMIDlet();
  39. } else if (command == helpcommand) {
  40. getDisplay().setCurrent(get_help());
  41. }
  42. } else if (displayable == help) {
  43. if (command == backfromhelp) {
  44. getDisplay().setCurrent(get_calcForm());
  45. }
  46. }
  47. }
  48.  
  49. public Display getDisplay() {
  50. return Display.getDisplay(this);
  51. }
  52.  
  53. public void exitMIDlet() {
  54. getDisplay().setCurrent(null);
  55. destroyApp(true);
  56. notifyDestroyed();
  57. }
  58.  
  59. public Form get_calcForm() {
  60. if (calcForm == null) {
  61. calcForm = new Form(null, new Item[] {
  62. new StringItem("pH Calculator",""),
  63. input_area_ka(),
  64. input_area_m(),
  65. input_area_ph()
  66. });
  67. calcForm.addCommand(get_exitcommand());
  68. calcForm.addCommand(get_helpcommand());
  69. calcForm.setCommandListener(this);
  70. }
  71. return calcForm;
  72. }
  73.  
  74. public TextField input_area_ka() {
  75. if (input_ka == null) {
  76. input_ka = new TextField("Ka", "0", 120, TextField.NUMERIC);
  77. }
  78. return input_ka;
  79. }
  80.  
  81. public TextField input_area_m() {
  82. if (input_m == null) {
  83. input_m = new TextField("Acid Concentration (M)", "0", 120, TextField.NUMERIC);
  84. }
  85. return input_m;
  86. }
  87.  
  88. public TextField input_area_ph() {
  89. if (input_ph == null) {
  90. input_ph = new TextField("pH of Solution", "0", 120, TextField.NUMERIC);
  91. }
  92. return input_ph;
  93. }
  94.  
  95. public Command get_exitcommand() {
  96. if (exitcommand == null) {
  97. exitcommand = new Command("Exit", Command.EXIT, 1);
  98. }
  99. return exitcommand;
  100. }
  101.  
  102. public Command get_helpcommand() {
  103. if (helpcommand == null) {
  104. helpcommand = new Command("Help", Command.HELP, 1);
  105. }
  106. return helpcommand;
  107. }
  108.  
  109. public Form get_help() {
  110. if (help == null) {
  111. help = new Form(null, new Item[] {
  112. new StringItem("Help Contents", ""),
  113. new StringItem("", "Enter Ka first. Then acid concentration or pH depending on which is known. Mark unknowns with 0.")
  114. });
  115. help.addCommand(get_backfromhelp());
  116. help.setCommandListener(this);
  117. }
  118. return help;
  119. }
  120.  
  121.  
  122. public Command get_backfromhelp() {
  123. if (backfromhelp == null) {
  124. backfromhelp = new Command("Back", Command.BACK, 1);
  125. }
  126. return backfromhelp;
  127. }
  128.  
  129. public void startApp() {
  130. initialize();
  131. }
  132.  
  133. public void pauseApp() {
  134. }
  135.  
  136. public void destroyApp(boolean unconditional) {
  137. }
  138. }

And the code I'm tring to port it from. Just so the previous makes more sense.

java Syntax (Toggle Plain Text)
  1. /*
  2.  * Main.java
  3.  *
  4.  * Created on November 14, 2007, 5:35 PM
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. package phcalc;
  11.  
  12. /**
  13.  *
  14.  * @author Eirik
  15.  */
  16. import java.io.*;
  17.  
  18. public class Main {
  19.  
  20. /** Creates a new instance of Main */
  21. public Main() {
  22. }
  23.  
  24. /**
  25.   * @param args the command line arguments
  26.   */
  27. public static void main(String[] args) throws IOException {
  28. double ph = 0, K = 0, M = 0;
  29. System.out.println("Enter Ka first. Then acid concentration or pH depending on which is known. Mark unknowns with 0.");
  30. try
  31. {
  32. InputStreamReader inputStreamReader = new InputStreamReader ( System.in );
  33. BufferedReader in = new BufferedReader ( inputStreamReader );
  34. System.out.println("Enter Ka ");
  35. String acidk = in.readLine();
  36. K = Double.parseDouble(acidk);
  37. System.out.println("Enter acid concentration ");
  38. String acidmolar = in.readLine();
  39. M = Double.parseDouble(acidmolar);
  40. System.out.println("Enter pH ");
  41. String solutionpH = in.readLine();
  42. ph = Double.parseDouble(solutionpH);
  43. }
  44. catch(IOException ioex)
  45. {
  46. System.out.println("Input error");
  47. System.exit(1);
  48. }
  49. catch(NumberFormatException nfex)
  50. {
  51. System.out.println("\"" +
  52. nfex.getMessage() +
  53. "\" is not numeric");
  54. System.exit(1);
  55. }
  56. if ( M == 0 ) {
  57. M = (((Math.pow((K+2*(Math.pow(10,-ph))),2))-K*K)/(4*K));
  58. System.out.println("Concentration: " + M );
  59. System.exit(0);
  60. }
  61. if ( ph == 0) {
  62. ph = (-Math.log10((-K+Math.sqrt((K*K)-4*-K*M))/2));
  63. System.out.println("pH: " + ph );
  64. System.exit(0);
  65.  
  66. }
  67. }
  68.  
  69. }
uzzors2k is offline   Reply With Quote