#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Help with my java program


    Hello, i am trying to calculate a value the set into a text feild but I Cant get it to work (the variable last). My code is included below, I just cant understand why it does not work. Look at the bottom or so 20 lines.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
     	class Calculator extends JFrame
     	{
     	JTextField firstnumber, operator, answer, secondnumber;  /*initialises the three text feilds*/
    
    
     	JMenu copyMenu;						       		/*creates the menu*/
     	JMenuItem swap, compare; 		           		/*creates the menu options*/
     	JMenuBar bar;                              		/*creates the menu bar*/
     	JButton calculate;
    
     		private JPanel labelTextPanel(String lab, JTextField txt)
    	{
    		JPanel pan = new JPanel();
    		pan.setLayout(new FlowLayout());
    		pan.add(new JLabel(lab));
    		pan.add(txt);
    		return pan;
    	}
    
     	public Calculator()
      	{
     	setTitle("calculator");
    	setSize(400, 290);
    	setLocation(400, 300);
    
    	Container c = getContentPane();		       		/*creates the content pane, adds colour and sets a flow layout*/
    	c.setBackground(Color.blue);
    	c.setLayout(new FlowLayout());
    
    	calculate = new JButton("calculate");
      	c.add(calculate);
    
    	firstnumber = new JTextField (20);
    	c.add(firstnumber);					      		 /*creates the three text feilds on the content box and set length of 20*/
    	JPanel p1 = labelTextPanel("first number", firstnumber);
    	c.add(p1);
    
    	operator = new JTextField (20);
    	c.add(operator);
    	JPanel p2 = labelTextPanel("operator", operator); /*calls the private method that creates the labels*/
    	c.add(p2);
    
    	secondnumber = new JTextField (20);
    	c.add(secondnumber);
    	JPanel p3 = labelTextPanel("secondnumber", secondnumber);
    	c.add(p3);
    
    	answer = new JTextField (20);
    	c.add(answer);
    	JPanel p4 = labelTextPanel("answer", answer);
    	c.add(p4);
    
    	swap = new JMenuItem("Swap");			   		/*adds the two menu options and gives there names*/
    	compare = new JMenuItem("compare");
    	copyMenu = new JMenu ("Copy and Compare"); 		/*creates the menu option button on bar and gives a title*/
    	copyMenu.add(swap);
    	copyMenu.add(compare);					   		/*adds the menu options*/
    
    	bar = new JMenuBar();
    	bar.add(copyMenu);						   		/*creates the bar and adds the previous menu option to the bar*/
    	setJMenuBar(bar);
    
    
    
    	swap.addActionListener(
    	new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	String temp1 = firstnumber.getText();			/*swaps the contents of the text boxes*/
    	String temp2 = secondnumber.getText();
    	firstnumber.setText(temp2);
    	secondnumber.setText(temp1);
    	}
    	});
    
    	calculate.addActionListener(
    	new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	int total;
    	int first;
    	int second;
    
    
    	String temp3 = firstnumber.getText();
    	String temp4 = secondnumber.getText();
    	String temp5 = operator.getText();
    	first = Integer.parseInt(temp3);
    	second = Integer.parseInt(temp4);
    
    	if (temp5 == "-")
    	{
    	total = first - second;
    	String last = Integer.toString(total);
    	answer = last.getText();
    	}
    
    	}
    	}
    	);
    
    	setVisible(true);						   		/*makes the whole GUI visible and allows the program to be exited*/
    	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      	}
    
     	}

    Code:
    	calculate.addActionListener(
    	new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	int total;
    	int first;
    	int second;
    
    
    	String temp3 = firstnumber.getText();
    	String temp4 = secondnumber.getText();
    	String temp5 = operator.getText();
    	first = Integer.parseInt(temp3);
    	second = Integer.parseInt(temp4);
    
    	if (temp5 == "-")
    	{
    	total = first - second;
    	String last = Integer.toString(total);
    
    	}
    
    	}
    	}
    	);
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    why it does not work
    Please explain.
    What does it do? What is wrong with what it does?
    If there are error messages, please post the full text of the error messages.

    Please edit your code and fix its indentation. Each nesting level of the logic should be indented 3-4 spaces. Your code is all in line making it hard to read the logic.

    For example there should not be 4 } all directly beneath each other.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0
    ok, as seen in the bottom peice of code a calculation is peformed after some parsing from strings to ints and then the result is placed back into a string.

    I have a text feild "answer" and I need to put the value of string "last" into this.

    there are no errors in this code or my attempts to pass the value to the feild.

    when the programs running I clicked calculate then nothing happens the feild stays blank, absent is the string "last" which I wnat there.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Try debugging your code by adding println() statements to show execution progress and how variable values are changing. For example:
    Add a: System.out.println("var=" + var);
    after every line where a variable is changed by an assignment statement or read into.

    The code you posted does not compile without errors.
    You should post good code or post the errors you get when you compile it.
    Last edited by NormR; March 22nd, 2012 at 01:47 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0
    To set the value of the text filed answer you just need to use it's setText() method.

    Just looking at the code the line
    Code:
    answer = last.getText();
    should fail to compile.
    What you probably intend is
    Code:
    answer.setText(last);
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by codeMonkey_1066
    To set the value of the text filed answer you just need to use it's setText() method.

    Just looking at the code the line
    Code:
    answer = last.getText();
    should fail to compile.
    What you probably intend is
    Code:
    answer.setText(last);
    that is exactly what I needed to do, thanks a lot got it working.

IMN logo majestic logo threadwatch logo seochat tools logo