944,126 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1570
  • Java RSS
Sep 29th, 2009
0

HELP<!>Program doesn't get user input from the 2nd Textfield(KAEquation)

Expand Post »
This is a program for generating a state table for flip flop circuits.
My problem is that the program does not get anything from the Textfield which gets the value for KAEquation, but for JAEquation, it works just fine.

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6. /**
  7.  * @author Jenielle Gabriel
  8.  *
  9.  */
  10. public class FFGen extends JFrame{
  11.  
  12. private String FlipflopType = "";
  13. private boolean isOneFlipflop = true;
  14. private boolean isInput = false;
  15. private boolean isOutput = false;
  16.  
  17. String JAEquation = "";
  18. String KAEquation = "";
  19. String JBEquation = "";
  20. String KBEquation = "";
  21. String OutputEquation = "";
  22.  
  23. public void First(){
  24.  
  25. /**
  26. * @param args
  27. */
  28. JLabel pleaseChooseFFType;
  29. String[] FlipflopTypes = {"JK", "RS", "D", "T"};;
  30. JComboBox FlipflopList;
  31.  
  32. JLabel pleaseChooseFFNum;
  33. ButtonGroup FlipflopNumberGroup;
  34. JRadioButtonMenuItem oneFlipflop;
  35. JRadioButtonMenuItem twoFlipflop;
  36.  
  37. JLabel pleaseCheck;
  38. JCheckBox oneInput;
  39. JCheckBox oneOutput;
  40.  
  41. JButton next;
  42.  
  43. JFrame first;
  44. JPanel firstConLeft;
  45. JPanel firstConRight;
  46.  
  47. FlipflopList = new JComboBox(FlipflopTypes);
  48. FlipflopList.addActionListener(
  49. new ActionListener(){
  50. public void actionPerformed(ActionEvent e){
  51. JComboBox cb = (JComboBox)e.getSource();
  52. FlipflopType = (String)cb.getSelectedItem();
  53. }
  54. }
  55. );
  56.  
  57. pleaseChooseFFType = new JLabel("Type of Flipflop:");
  58. oneFlipflop = new JRadioButtonMenuItem ("1");
  59. oneFlipflop.addActionListener(
  60. new ActionListener(){
  61. public void actionPerformed(ActionEvent e){
  62. isOneFlipflop = true;
  63. }
  64. }
  65. );
  66. twoFlipflop = new JRadioButtonMenuItem ("2");
  67. twoFlipflop.addActionListener(
  68. new ActionListener(){
  69. public void actionPerformed(ActionEvent e){
  70. isOneFlipflop = false;
  71. }
  72. }
  73. );
  74.  
  75. pleaseChooseFFNum = new JLabel("Number of Flipflops:");
  76. FlipflopNumberGroup = new ButtonGroup();
  77. FlipflopNumberGroup.add(oneFlipflop);
  78. FlipflopNumberGroup.add(twoFlipflop);
  79.  
  80. pleaseCheck = new JLabel("There is an:");
  81. oneInput = new JCheckBox ("Input");
  82. oneInput.addItemListener(
  83. new ItemListener(){
  84. public void itemStateChanged(ItemEvent e) {
  85. if (e.getStateChange() == ItemEvent.SELECTED)
  86. isInput = true;
  87. else if (e.getStateChange() == ItemEvent.DESELECTED)
  88. isInput = false;
  89. }
  90. }
  91. );
  92.  
  93. oneOutput = new JCheckBox ("Output");
  94. oneOutput.addItemListener(
  95. new ItemListener(){
  96. public void itemStateChanged(ItemEvent e) {
  97. if (e.getStateChange() == ItemEvent.SELECTED)
  98. isOutput = true;
  99. else if (e.getStateChange() == ItemEvent.DESELECTED)
  100. isOutput = false;
  101. }
  102. }
  103. );
  104.  
  105. next = new JButton ("Next");
  106. next.addActionListener(
  107. new ActionListener(){
  108. public void actionPerformed(ActionEvent e){
  109. if(FlipflopType == "JK")
  110. JK();
  111. }
  112. }
  113. );
  114.  
  115. first = new JFrame();
  116.  
  117. firstConLeft = new JPanel();
  118. firstConRight = new JPanel();
  119. firstConRight.setLayout(new GridLayout(9,1,0,0));
  120. firstConRight.add(pleaseChooseFFType);
  121. firstConRight.add(FlipflopList);
  122. firstConRight.add(pleaseChooseFFNum);
  123. firstConRight.add(oneFlipflop);
  124. firstConRight.add(twoFlipflop);
  125. firstConRight.add(pleaseCheck);
  126. firstConRight.add(oneInput);
  127. firstConRight.add(oneOutput);
  128. firstConRight.add(next);
  129.  
  130. JPanel Empty = new JPanel();
  131. Empty.setLayout(new GridLayout(1,2));
  132. Empty.add(firstConLeft);
  133. Empty.add(firstConRight);
  134. first.add(Empty);
  135.  
  136. first.setSize(250,235);
  137. first.setVisible(true);
  138. //first.validate();
  139.  
  140. }
  141.  
  142. public void JK(){
  143.  
  144. JFrame JKFrame = new JFrame();
  145.  
  146. JButton A = new JButton("A");
  147. JButton B = new JButton("B");
  148. JButton x = new JButton("x");
  149. JButton open = new JButton ("(");
  150. JButton close = new JButton (")");
  151. JButton and = new JButton("*");
  152. JButton or = new JButton("+");
  153.  
  154. final JTextField JAEq = new JTextField();
  155. final JTextField KAEq = new JTextField();
  156. final JTextField JBEq = new JTextField();
  157. final JTextField KBEq = new JTextField();
  158. final JTextField OutputEq = new JTextField();
  159.  
  160. JPanel JKLeftPanel = new JPanel();
  161. JPanel JKRightPanel = new JPanel();
  162. JKRightPanel.setLayout(new GridLayout(7,2));
  163. JKRightPanel.add(new JLabel("Enter the equations. "));
  164. JKRightPanel.add(new JLabel("Observe Proper Syntax."));
  165. JKRightPanel.add(new JLabel("JA = "));
  166. JKRightPanel.add(JAEq);
  167.  
  168. JKRightPanel.add(new JLabel("JB = "));
  169. JKRightPanel.add(JBEq);
  170. JKRightPanel.add(new JLabel("KA = "));
  171. JKRightPanel.add(KAEq);
  172. JKRightPanel.add(new JLabel("KB = "));
  173. JKRightPanel.add(KBEq);
  174. JKRightPanel.add(new JLabel("x = "));
  175. JKRightPanel.add(OutputEq);
  176. JKRightPanel.add(new JLabel("Generate: "));
  177. JButton next = new JButton("next");
  178. JKRightPanel.add(next);
  179. next.addActionListener(
  180. new ActionListener(){
  181. public void actionPerformed(ActionEvent e)
  182. {
  183. JAEquation = JAEq.getText();
  184. JBEquation = JAEq.getText();
  185. KAEquation = KAEq.getText();
  186. KBEquation = KAEq.getText();
  187. OutputEquation = OutputEq.getText();
  188. genarateJK();
  189. }
  190. }
  191. );
  192.  
  193. JKFrame.add(JKRightPanel);
  194. JKFrame.setSize(250,235);
  195. JKFrame.setVisible(true);
  196. }
  197.  
  198. public void genarateJK(){
  199.  
  200. JFrame JKTableFrame = new JFrame();
  201.  
  202. int c = 0;
  203. int nextA[] = {0,0}; //Storage for next state A (has default values)
  204. int nextB[]= {0,0}; //Storage for next state B (has default values)
  205.  
  206. int JA[] = {0,0}; //Storage for JA Values
  207. int JB[] = {0,0}; //Storage for JB Values
  208. int KA[] = {0,0}; //Storage for KA Values
  209. int KB[] = {0,0}; //Storage for KB Values
  210.  
  211.  
  212. if(isOneFlipflop && !isInput){ // one flipflop, no input, no output
  213. String[] A = {"1","0"};
  214. c = 2;
  215.  
  216. for(int j=0; j<c; j++){
  217.  
  218. //Complement of A
  219. String AComp = "";
  220. if(A[j] == "0")
  221. AComp = "1";
  222. else
  223. AComp = "0";
  224.  
  225. //replace JA Formula with current value of A'
  226. String subsJA = replace(JAEquation,"A'",AComp);
  227. while(!subsJA.equals(replace(JAEquation,"A'",AComp))){
  228. subsJA = replace(JAEquation,"A",A[j]);
  229. }
  230.  
  231. //replace JA Formula with current value of A
  232. subsJA = replace(JAEquation,"A",A[j]);
  233. while(!subsJA.equals(replace(JAEquation,"A",A[j]))){
  234. subsJA = replace(JAEquation,"A",A[j]);
  235. }
  236.  
  237.  
  238. //replace KA Formula with current value of A'
  239. String subsKA = replace(KAEquation,"A'",AComp);
  240. while(!subsKA.equals(replace(KAEquation,"A",AComp))){
  241. subsKA = replace(KAEquation,"A",A[j]);
  242. }
  243.  
  244.  
  245. //replace KA Formula with current value of A
  246. subsKA = replace(KAEquation,"A",A[j]);
  247. while(!subsKA.equals(replace(KAEquation,"A",A[j]))){
  248. subsKA = replace(KAEquation,"A",A[j]);
  249. }
  250.  
  251.  
  252. try{
  253.  
  254. Parser parser = new Parser();
  255. //compute the value of JA
  256. int computedJAValue = (int)parser.evaluate(subsJA);
  257. JA[j] = computedJAValue;
  258.  
  259. //compute the value of KA
  260. int computedKAValue = (int)parser.evaluate(subsKA);
  261. KA[j] = computedKAValue;
  262.  
  263. }catch(Exception e){
  264. //testlang.setText(e.getMessage());
  265. }
  266.  
  267. if(JA[j] == 0 && KA[j] == 0){
  268. nextA[j] = Integer.parseInt(A[j]);
  269. }
  270. else if(JA[j] == 0 && KA[j] == 1){
  271. nextA[j] = 0;
  272. }
  273. else if(JA[j] == 1 && KA[j] == 0){
  274. nextA[j] = 1;
  275. }
  276. else if(JA[j] == 1 && KA[j] == 1){
  277. if(A[j] == "0")
  278. nextA[j] = 1;
  279. else if(A[j] == "1")
  280. nextA[j] = 0;
  281. }
  282.  
  283. }//End for loop
  284.  
  285.  
  286.  
  287.  
  288.  
  289. JKTableFrame.setLayout(new GridLayout(1,4));
  290. JKTableFrame.add(new JLabel("<html>Present<br>States<br>"+A[0]+"<br>"+A[1]));
  291. JKTableFrame.add(new JLabel("<html>JA<br><br>"+JA[0]+"<br>"+JA[1]));
  292. JKTableFrame.add(new JLabel(JAEquation));
  293. //JKTableFrame.add(new JLabel("<html>KA<br><br>"+KA[0]+"<br>"+KA[1]));
  294. JKTableFrame.add(new JLabel("<html>Next<br>States<br>"+nextA[0]+"<br>"+nextA[1]));
  295.  
  296. }//End one flipflop, no input, no output
  297.  
  298.  
  299. JKTableFrame.setSize(250,235);
  300. JKTableFrame.setVisible(true);
  301.  
  302. }
  303.  
  304. static String replace(String str, String pattern, String replace) {
  305. int s = 0;
  306. int e = 0;
  307. StringBuffer result = new StringBuffer();
  308.  
  309. while ((e = str.indexOf(pattern, s)) >= 0) {
  310. result.append(str.substring(s, e));
  311. result.append(replace);
  312. s = e+pattern.length();
  313. }
  314. result.append(str.substring(s));
  315. return result.toString();
  316. }
  317.  
  318.  
  319. public static void main(String[] args) {
  320. // TODO Auto-generated method stub
  321. FFGen gui = new FFGen();
  322. gui.First();
  323. gui.repaint();
  324. }
  325.  
  326. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
einjelle is offline Offline
9 posts
since Jun 2009
Sep 29th, 2009
0

Re: HELP<!>Program doesn't get user input from the 2nd Textfield(KAEquation)

JAEquation = JAEq.getText();
JBEquation = JAEq.getText();

Is this second line a mis-type?

(ditto for
KAEquation = KAEq.getText();
KBEquation = KAEq.getText(); )
Last edited by JamesCherrill; Sep 29th, 2009 at 12:48 pm.
Featured Poster
Reputation Points: 1938
Solved Threads: 953
Posting Expert
JamesCherrill is offline Offline
5,809 posts
since Apr 2008
Sep 30th, 2009
0

Re: HELP<!>Program doesn't get user input from the 2nd Textfield(KAEquation)

O, yah. Thanks I fixed it already..
Now I have another problem..
Can't think of a way to for parsing XOR equations
like this:
/*XOR = & */
(A&(B&C))
Reputation Points: 10
Solved Threads: 0
Newbie Poster
einjelle is offline Offline
9 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Multiple Inheritence
Next Thread in Java Forum Timeline: XOR Equation Expansion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC