Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    I have updated my code yet again and it still gives me an error when I run it.. Here is the code:

    Java Code:
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class CalculatorTest extends JApplet implements ActionListener
    {
    
    
    		JTextField input01;
    		JTextField input02;
    		JTextField oper01;
    		JTextField output01;
    		char operation;
    		char tempop;
    		double input1;
    		double input2;
    		double output1;
    
    		public void init()
    		{
    
    
    				Container contentPane = getContentPane();
    				contentPane.setLayout(new FlowLayout());
    
    				JLabel instructions = new JLabel("Enter two inputs and an operation to get the result.");
    				contentPane.add(instructions);
    
    				input01 = new JTextField("", 25);
    				input01.setText(" ");
    				contentPane.add(input01);
    
    				input02 = new JTextField("", 25);
    				input02.setText(" ");
    				contentPane.add(input02);
    
    				oper01 = new JTextField("", 25);
    				oper01.setText(" ");
    				contentPane.add(oper01);
    
    				output01 = new JTextField("", 25);
    				output01.setText(" ");
    				contentPane.add(output01);
    
    				JButton updateButton = new JButton("Update");
    				updateButton.addActionListener(this);
    				contentPane.add(updateButton);
    
    				JButton resetButton = new JButton("Reset");
    				resetButton.addActionListener(this);
    				contentPane.add(resetButton);
    		}
    
    
    				public void actionPerformed(ActionEvent e)
    				{
    
    						String actionCommand = e.getActionCommand();
    						{
    
    						if(actionCommand.equals("Update"))
    						{
    
    							[B]input1 = Double.parseDouble(input01.getText());
    							input2 = Double.parseDouble(input02.getText());
    							tempop = oper01.charAt(0);
    							oper01 = tempop;[/B]
    
    							if(operation=='+')
    							{
    
    							output1 = input1 + input2;
    
    							}
    
    							else if(operation=='-')
    							{
    
    							output1 = input1 - input2;
    
    							}
    
    							else if(operation=='*')
    							{
    
    							output1 = input1 * input2;
    
    							}
    
    							else if(operation=='/')
    							{
    
    							output1 = input1 / input2;
    
    							}
    
    							else
    
    							output1 = 0.0;
    
    						}
    
    
    						else if(actionCommand.equals("Reset"))
    						{
    
    							input01.setText("");
    							input02.setText("");
    							oper01.setText("");
    							output01.setText("");
    						}
    
    						else
    						{
    
    
    							input01.setText("");
    							input02.setText("");
    							oper01.setText("");
    							output01.setText("");
    				}
    			}
    	}
    }
    I bolded the part I modified.. I did the parseDouble to get the String based values from the textbox and change it to double value. I don't seem to get an error when I do that.. The error comes when I add the tempop = oper01.charAt(0); I really need to finish this assignment this weekend.. I am really confused.. please help.. thanks in advance :)

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Asvin View Post
    I bolded the part I modified.. I did the parseDouble to get the String based values from the textbox and change it to double value. I don't seem to get an error when I do that.. The error comes when I add the tempop = oper01.charAt(0); I really need to finish this assignment this weekend.. I am really confused.. please help.. thanks in advance :)
    You must get compilation errors because oper01 is a JTextField and such a field doesn't have a charAt( ... ) method. It has a getText() method that hands you the entire String content. You can call the charAt( ... ) method on a String. The result will be a single char. The fourth line in bold also is incorrect because you can't assign a single character to a JTextField.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Problem with setColumns in JTextField
    By shai in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 06:41 AM
  2. Manually position JTextfield problem
    By castiel in forum AWT / Swing
    Replies: 3
    Last Post: 02-20-2010, 06:24 AM
  3. Applet JTextField Size Problem
    By a13w in forum New To Java
    Replies: 4
    Last Post: 09-19-2009, 08:52 AM
  4. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  5. Got problem with JtextField
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2008, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •