Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 18th, 2007, 4: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
Old Nov 18th, 2007, 6:03 AM   #2
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: olympia,WA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: Help needed with using TextFields as user input

I do not have ME installed so i cannot compile your code. what errors do you get?

I do not have experiance with ME and MIDlets but I have some experiance with awt and swing. If you are using TextFields you need to import them form import java.awt.TextField. For swing gui's to get text from a JTextField use .getText
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Nov 18th, 2007, 2:15 PM   #3
uzzors2k
Newbie
 
Join Date: Dec 2006
Location: Norway
Posts: 4
Rep Power: 0 uzzors2k is on a distinguished road
Re: Help needed with using TextFields as user input

I forgot to mention, the code above compiles and works, I just want to extract the values from the textfields and put them into variables. There is no code in place to do so yet. The IDE I'm using is Netbeans. I've tried using .getText and declaring a JTextField, but neither work. I was finally able to get a number from a textfield, but decimal numbers cause the program to crash.

java Syntax (Toggle Plain Text)
  1. K = Double.parseDouble(((TextField)input_ka).getString());

Is there some kind of midlet limitation, which prevents certain codes from working? It seems I can only use .getString, .setString and not .getText, .setText.
uzzors2k is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
input data from user into array lilmul123 C++ 5 May 26th, 2006 9:29 AM
User input jayme C++ 38 Nov 25th, 2005 7:27 PM
Should i use fgets to set user input to a variable? linuxpimp20 C 3 Nov 25th, 2005 5:04 AM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
Problem read'ing from file and user input jmsilver Bash / Shell Scripting 3 May 20th, 2005 2:35 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC