Results 1 to 13 of 13
Thread: Coding newbie, please help
- 10-08-2010, 02:16 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
Coding newbie, please help
Im creating an applet that adds two integers. I have it layed out how I want it to, but I dont know where to add the actual method, or how im supposed to set it up.
Java Code:/* document segment filename: ActionApplet author: Walker date: october.2010 */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class addNumbers extends JApplet implements ActionListener { /* ----------------------------- declarations */ // color objects Color white = new Color(255, 255, 255); Color black = new Color(0, 0, 0); // components JLabel inputLabelJLabel; JTextField inputLabelJTextField; JLabel outputLabelJLabel; JTextField outputLabelJTextField; JLabel finalLabelJLabel; JTextField finalLabelTextField; JButton enterJButton; JButton clearJButton; // variables String inputName; String outputName; public void init() { setLayout(null); setSize(400, 400); /* ------------------- initialization */ inputLabelJLabel = new JLabel(); inputLabelJLabel.setBounds(100, 50, 150, 20); inputLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12)); inputLabelJLabel.setText("Enter First Integer"); inputLabelJLabel.setForeground(black); inputLabelJLabel.setHorizontalAlignment(JLabel.LEFT); add(inputLabelJLabel); inputLabelJTextField = new JTextField(); inputLabelJTextField.setBounds(230, 50, 150, 20); inputLabelJTextField.setFont(new Font("Default", Font.PLAIN, 12)); inputLabelJTextField.setHorizontalAlignment(JTextField.CENTER); inputLabelJTextField.setForeground(black); inputLabelJTextField.setBackground(white); inputLabelJTextField.setEditable(true); add(inputLabelJTextField); outputLabelJLabel = new JLabel(); outputLabelJLabel.setBounds(100, 80, 150, 20); outputLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12)); outputLabelJLabel.setText("Enter Second Interger"); outputLabelJLabel.setForeground(black); outputLabelJLabel.setHorizontalAlignment(JLabel.LEFT); add(outputLabelJLabel); outputLabelJTextField = new JTextField(); outputLabelJTextField.setBounds(230, 80, 150, 20); outputLabelJTextField.setFont(new Font("Default", Font.PLAIN, 12)); outputLabelJTextField.setHorizontalAlignment(JTextField.CENTER); outputLabelJTextField.setForeground(black); outputLabelJTextField.setBackground(white); outputLabelJTextField.setEditable(true); add(outputLabelJTextField); finalLabelJLabel = new JLabel(); finalLabelJLabel.setBounds(100, 110, 150, 20); finalLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12)); finalLabelJLabel.setText("Addition Results"); finalLabelJLabel.setForeground(black); finalLabelJLabel.setHorizontalAlignment(JLabel.LEFT); add(finalLabelJLabel); finalLabelTextField = new JTextField(); finalLabelTextField.setBounds(230, 110, 150, 20); finalLabelTextField.setFont(new Font("Default", Font.PLAIN, 12)); finalLabelTextField.setHorizontalAlignment(JTextField.CENTER); finalLabelTextField.setForeground(black); finalLabelTextField.setBackground(white); finalLabelTextField.setEditable(false); add(finalLabelTextField); enterJButton = new JButton(); enterJButton.setBounds(100, 300, 100, 20); enterJButton.setFont(new Font("Default", Font.PLAIN, 12)); enterJButton.setText("Enter"); enterJButton.setForeground(black); enterJButton.setBackground(white); add(enterJButton); enterJButton.addActionListener(this); clearJButton = new JButton(); clearJButton.setBounds(210, 300, 100, 20); clearJButton.setFont(new Font("Default", Font.PLAIN, 12)); clearJButton.setText("Clear"); clearJButton.setForeground(black); clearJButton.setBackground(white); add(clearJButton); clearJButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); /* the following lines of code are commented out, remove the // to un-comment after the components are initialized */ if(obj == enterJButton) { getFirstMethod(); } else if(obj == clearJButton) { clearAll(); } } public void getFirstMethod() { } public void getOutput() { outputLabelJTextField.setText("" + inputName); } public void clearAll() { inputLabelJTextField.setText(""); inputLabelJTextField.requestFocusInWindow(); outputLabelJTextField.setText(""); } }
-
A general programming technique that will help you immensely: break the problem down and then solve each sub-problem one at a time. So tell me:
1) Which method should contain the code for the addition?
2) Where are the numbers to be added held?
3) How do you extract the Strings from the above JTextFields?
4) How do you parse these Strings to numbers?
5) How do you display the result?
So please tell us what you think are the best answers to the above questions and also which step is the one that is most troubling you, and we'll be better able to help you.
- 10-08-2010, 05:16 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
Thanks for the reply, this is my first app and well, ive only been using java for a few weeks. Online course + bad book= hard time haha.
1. I would think that "Public void getFirstMethod" should contain the the code. Or maybe create a specific method for it?
2.In the decleration or Initialization
3. with the "get" method?
4. Nothing has been mentioned so far about "parse"
5. displayOutput() ??
-
Thanks for answering. Here are my thoughts on this, and please let me know if anything doesn't make sense:
1) Which method should contain the code for the addition?
You said "I would think that "Public void getFirstMethod" should contain the the code." and I think that you're right on the money here. This is the method that is called when actionPerformed method, the method that gets called on button push, identifies the source button (in actionPerformed called obj) as the enterJButton:
So indeed, you want to put your calculation methods in here.Java Code:if(obj == enterJButton) { getFirstMethod(); }
2) Where are the numbers to be added held?
You said, "In the decleration or Initialization".
I was looking more for which Swing components will hold the numbers to be added, or in other words, where will the user of your program enter these numbers. The answer I think should be in the inputLabelJTextField and the outputLabelJTextField. Perhaps these variable names should be changed to inputField1 and inputField2.
3) How do you extract the Strings from the above JTextFields?
You said, "with the "get" method?"
And indeed this is correct. Specifically, you'll call getText() on the JTextFields holding the inputted numbers which will return Strings.
4) How do you parse these Strings to numbers?
You stated, "Nothing has been mentioned so far about "parse""
OK, it's time that we mentioned it. As stated above, the JTextFields above will hold the user's input, and when you call getText() on either of them, a String will be returned. This String will need to be translated or "parsed" into a number, if you want ints, then they will have to be converted into ints. You would do this using the Integer.parseInt(...) method. e.g.,
Java Code:// if our input JTextFields were named inputField1 and inputField2 // get the input Strings from the JTextFields String inputText1 = inputField1.getText(); String inputText2 = inputField2.getText(); // parse or translate the Strings into ints int inputInt1 = Integer.parseInt(inputText1); int inputInt2 = Integer.parseInt(inputText2); // then add them int result = //... here you'd add your ints....
5) How do you display the result?
You said "displayOutput() ??"
Probably the best thing to do here is to display the result of your addition in another Swing component such as a JLabel or other JTextField, perhaps your finalLabelTextField. You would set the text of this JTextField by calling setText(....). Note that the parameter passed into this method must be a String, and you can translate your int result into a String by using either String.valueOf(result), or you could use a short cut by adding quotes before the int variable so that the compiler will automatically change the into into a String:
Much luck and hope this helps!Java Code:// if the output JTextField is called outputField: outputField.setText(String.valueOf(result)); // first way to do this outputField.setText("" + result); // second way to do this
- 10-10-2010, 06:24 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
Thanks for your reply! New Code! Hopefully easier to understand. Im getting and incompatible types at line 145 (the first parse)
Java Code:/* document segment filename: ActionApplet author: Walker date: october.2010 */ import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.applet.*; public class addNumbers4 extends JApplet implements ActionListener { /* ----------------------------- declarations */ // color objects Color white = new Color(255, 255, 255); Color black = new Color(0, 0, 0); // components JLabel firstInteger; JTextField firstIntegerText; JLabel secondInteger; JTextField secondIntegerText; JLabel finalLabelJLabel; JTextField finalLabelTextField; JButton enterJButton; JButton clearJButton; // variables String inputName; String outputName; int num1; int num2; public void init() { setLayout(null); setSize(400, 400); /* ------------------- initialization */ firstInteger = new JLabel(); firstInteger.setBounds(100, 50, 150, 20); firstInteger.setFont(new Font("Default", Font.PLAIN, 12)); firstInteger.setText("Enter First Integer"); firstInteger.setForeground(black); firstInteger.setHorizontalAlignment(JLabel.LEFT); add(firstInteger); firstIntegerText = new JTextField(); firstIntegerText.setBounds(230, 50, 150, 20); firstIntegerText.setFont(new Font("Default", Font.PLAIN, 12)); firstIntegerText.setHorizontalAlignment(JTextField.CENTER); firstIntegerText.setForeground(black); firstIntegerText.setBackground(white); firstIntegerText.setEditable(true); add(firstIntegerText); secondInteger = new JLabel(); secondInteger.setBounds(100, 80, 150, 20); secondInteger.setFont(new Font("Default", Font.PLAIN, 12)); secondInteger.setText("Enter Second Interger"); secondInteger.setForeground(black); secondInteger.setHorizontalAlignment(JLabel.LEFT); add(secondInteger); secondIntegerText = new JTextField(); secondIntegerText.setBounds(230, 80, 150, 20); secondIntegerText.setFont(new Font("Default", Font.PLAIN, 12)); secondIntegerText.setHorizontalAlignment(JTextField.CENTER); secondIntegerText.setForeground(black); secondIntegerText.setBackground(white); secondIntegerText.setEditable(true); add(secondIntegerText); finalLabelJLabel = new JLabel(); finalLabelJLabel.setBounds(100, 110, 150, 20); finalLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12)); finalLabelJLabel.setText("Addition Results"); finalLabelJLabel.setForeground(black); finalLabelJLabel.setHorizontalAlignment(JLabel.LEFT); add(finalLabelJLabel); finalLabelTextField = new JTextField(); finalLabelTextField.setBounds(230, 110, 150, 20); finalLabelTextField.setFont(new Font("Default", Font.PLAIN, 12)); finalLabelTextField.setHorizontalAlignment(JTextField.CENTER); finalLabelTextField.setForeground(black); finalLabelTextField.setBackground(white); finalLabelTextField.setEditable(false); add(finalLabelTextField); enterJButton = new JButton(); enterJButton.setBounds(100, 300, 100, 20); enterJButton.setFont(new Font("Default", Font.PLAIN, 12)); enterJButton.setText("Enter"); enterJButton.setForeground(black); enterJButton.setBackground(white); add(enterJButton); enterJButton.addActionListener(this); clearJButton = new JButton(); clearJButton.setBounds(210, 300, 100, 20); clearJButton.setFont(new Font("Default", Font.PLAIN, 12)); clearJButton.setText("Clear"); clearJButton.setForeground(black); clearJButton.setBackground(white); add(clearJButton); clearJButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); /* the following lines of code are commented out, remove the // to un-comment after the components are initialized */ if(obj == enterJButton) { getFirstInteger(); } else if(obj == clearJButton) { clearAll(); } } public void getFirstInteger() { firstInteger = Integer.parseInt(firstIntegerText.getText()); getSecondInteger(); } public void getSecondInteger() { secondInteger = Integer.parseInt(secondIntegerText.getText()); getAdditionResults(); } public void getAdditionResults() { additionResults = firstInteger + secondInteger; displayResults(); } public void displayResults() { additionResultsJTextField.setText("" + additionResults); } public void clearAll() { firstIntegerText.setText(""); firstIntegerText.requestFocusInWindow(); secondIntegerText.setText(""); finalLabelTextField.setText(""); } }Last edited by skuzzie; 10-10-2010 at 07:37 PM.
- 10-10-2010, 07:38 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
Updated code.
-
What is the actual error message? Please post it here. It will tell you exactly what is causing your program not to compile. Also, your code is more complex than it has to be as the four methods from getFirstInteger to displayResults should be coalesced into one method.
Last edited by Fubarable; 10-10-2010 at 07:45 PM.
- 10-10-2010, 07:52 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
Heres my current code. At the bottom where "getFinalLabel" I have "first integer + second integer" it says I cant use the + operator.
Java Code:/* document segment filename: ActionApplet author: Walker date: october.2010 */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class addNumbers4 extends JApplet implements ActionListener { /* ----------------------------- declarations */ // color objects Color white = new Color(255, 255, 255); Color black = new Color(0, 0, 0); // components JLabel firstInteger; JTextField firstIntegerText; JLabel secondInteger; JTextField secondIntegerText; JLabel finalLabelJLabel; JTextField finalLabelTextField; JButton enterJButton; JButton clearJButton; // variables String inputName; String outputName; int num1; int num2; public void init() { setLayout(null); setSize(400, 400); /* ------------------- initialization */ firstInteger = new JLabel(); firstInteger.setBounds(100, 50, 150, 20); firstInteger.setFont(new Font("Default", Font.PLAIN, 12)); firstInteger.setText("Enter First Integer"); firstInteger.setForeground(black); firstInteger.setHorizontalAlignment(JLabel.LEFT); add(firstInteger); firstIntegerText = new JTextField(); firstIntegerText.setBounds(230, 50, 150, 20); firstIntegerText.setFont(new Font("Default", Font.PLAIN, 12)); firstIntegerText.setHorizontalAlignment(JTextField.CENTER); firstIntegerText.setForeground(black); firstIntegerText.setBackground(white); firstIntegerText.setEditable(true); add(firstIntegerText); secondInteger = new JLabel(); secondInteger.setBounds(100, 80, 150, 20); secondInteger.setFont(new Font("Default", Font.PLAIN, 12)); secondInteger.setText("Enter Second Interger"); secondInteger.setForeground(black); secondInteger.setHorizontalAlignment(JLabel.LEFT); add(secondInteger); secondIntegerText = new JTextField(); secondIntegerText.setBounds(230, 80, 150, 20); secondIntegerText.setFont(new Font("Default", Font.PLAIN, 12)); secondIntegerText.setHorizontalAlignment(JTextField.CENTER); secondIntegerText.setForeground(black); secondIntegerText.setBackground(white); secondIntegerText.setEditable(true); add(secondIntegerText); finalLabelJLabel = new JLabel(); finalLabelJLabel.setBounds(100, 110, 150, 20); finalLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12)); finalLabelJLabel.setText("Addition Results"); finalLabelJLabel.setForeground(black); finalLabelJLabel.setHorizontalAlignment(JLabel.LEFT); add(finalLabelJLabel); finalLabelTextField = new JTextField(); finalLabelTextField.setBounds(230, 110, 150, 20); finalLabelTextField.setFont(new Font("Default", Font.PLAIN, 12)); finalLabelTextField.setHorizontalAlignment(JTextField.CENTER); finalLabelTextField.setForeground(black); finalLabelTextField.setBackground(white); finalLabelTextField.setEditable(false); add(finalLabelTextField); enterJButton = new JButton(); enterJButton.setBounds(100, 300, 100, 20); enterJButton.setFont(new Font("Default", Font.PLAIN, 12)); enterJButton.setText("Enter"); enterJButton.setForeground(black); enterJButton.setBackground(white); add(enterJButton); enterJButton.addActionListener(this); clearJButton = new JButton(); clearJButton.setBounds(210, 300, 100, 20); clearJButton.setFont(new Font("Default", Font.PLAIN, 12)); clearJButton.setText("Clear"); clearJButton.setForeground(black); clearJButton.setBackground(white); add(clearJButton); clearJButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); /* the following lines of code are commented out, remove the // to un-comment after the components are initialized */ if(obj == enterJButton) { getFirstInteger(); } else if(obj == clearJButton) { clearAll(); } } public void getFirstInteger() { num1 = Integer.parseInt(firstIntegerText.getText()); getSecondInteger(); } public void getSecondInteger() { num2 = Integer.parseInt(secondIntegerText.getText()); getFinalLabelJLabel(); } public void getFinalLabelJLabel() { finalLabelJLabel = firstInteger + secondInteger; displayResults(); } public void displayResults() { additionResultsJTextField.setText("" + additionResults); } public void clearAll() { firstIntegerText.setText(""); firstIntegerText.requestFocusInWindow(); secondIntegerText.setText(""); finalLabelTextField.setText(""); } }
-
- 10-10-2010, 08:21 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
addNumbers4.java:156: operator + cannot be applied to javax.swing.JLabel,javax.swing.JLabel
-
As I thought: the error message is telling you exactly what is wrong, that you are trying to add one JLabel to another. Check your code and I'm sure you'll find that both firstInteger and secondInteger are JLabels, and so trying to add these makes no sense, as it's like trying to add your dog and your cat. You can only do addition with numbers.
Again, consolidate your addition code into one method, get the text from the appropriate JTextFields, convert to number by parsing (as discussed above), and then add these together. Then convert the sum into a String (as discussed above) and set the text of the JLabel holding the result.
- 10-11-2010, 02:08 AM #12
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
Im lost haha. Ive changed it so im adding int num1, num2. But I still dont understand where im supposed to add the code to get the string and then the parse. I mean isnt this supposed to do that
Java Code:num1 = Integer.parseInt(firstIntegerText.getText()); getSecondInteger();
-
Let me try to spell out my recommendations more clearly:
1) Let's name things correctly so they make sense. For instance change the name of enterButton to addButton since after all, pressing it is supposed to cause two ints to be added.
2) Likewise, change the name of the getFirstInteger method to addButtonActionPerformed since this is the method you want called when the addButton is pressed.
3) As I said before, simplify your code and get rid of the getSecondInteger method, the getAdditionResults, method and the displayResults method as we're going to do all this stuff in the addButtonActionPerformed method.
4) In the addButtonActionPerformed method declare two int variables and fill them with the results from Integer.parseInt called on the text from your two JTextFields respectively.
5) Add the two ints and place the results in another int, say called sum.
6) Use this sum result to set the text of either a JLabel or a JTextField, whatever you want to display the results in. You will need to translate sum into a String and this can be done via String.valueOf(sum).
7) Again points 4) through 6) will be done in the addButtonActionPerformed method.
Similar Threads
-
Need help coding
By ace_03 in forum New To JavaReplies: 2Last Post: 11-25-2009, 05:16 PM -
help with java coding
By helpisontheway in forum New To JavaReplies: 4Last Post: 11-14-2009, 07:00 AM -
Coding help
By Java_Fanatic in forum New To JavaReplies: 7Last Post: 10-15-2009, 04:37 AM -
Better coding
By tomiu in forum New To JavaReplies: 1Last Post: 04-09-2009, 07:19 PM -
coding help
By accies76 in forum New To JavaReplies: 5Last Post: 11-12-2008, 08:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks