Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2006, 1:33 PM   #1
Java-Head
Newbie
 
Join Date: Mar 2006
Posts: 4
Rep Power: 0 Java-Head is on a distinguished road
Unhappy Calculator Problem

Hi, I'm trying to code a calculator by using this following code:
**begning part removed for length reasons**

	one.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"1");
				one.setPreferredSize(new Dimension(x, y));
			}
		});
		two.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"2");
				two.setPreferredSize(new Dimension(x, y));
			}
		});
		three.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"3");
				three.setPreferredSize(new Dimension(x, y));
			}
		});
				four.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"4");
				four.setPreferredSize(new Dimension(x, y));
			}
		});
				five.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"5");
				five.setPreferredSize(new Dimension(x, y));
			}
		});
				six.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"6");
				six.setPreferredSize(new Dimension(x, y));
			}
		});
			seven.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"7");
				seven.setPreferredSize(new Dimension(x, y));
			}
		});
			eight.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"8");
				eight.setPreferredSize(new Dimension(x, y));
			}
		});
			nine.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"9");
				nine.setPreferredSize(new Dimension(x, y));
			}
		});
			zero.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText(aTextField1.getText()+"0");
				zero.setPreferredSize(new Dimension(x, y));
			}
		});
		aButton1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (add.equals(add))
				{
					String a = new String (aTextField1.getText());
					aTextField1.setText("")
				;}
			}
		});	
			aButton2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (minus.equals(minus))
				{
					String a = new String (aTextField1.getText());
					aTextField1.setText("")
				;}
			}
		});
			aButton3.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (multiply.equals(multiply))
				{
					String a = new String (aTextField1.getText());
					aTextField1.setText("")
				;}
			}
		});
			aButton4.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (divide.equals(divide))
				{
					String a = new String (aTextField1.getText());
					aTextField1.setText("")
				;}
			}
		});
		clear.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText("");
				zero.setPreferredSize(new Dimension(x, y));
			}
		});
		equals.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				if (add.equals(add))
			{
			String b = new String (aTextField1.getText() + a);
			aTextField1.setText(b);
			}
		}
		});
	}
}

Basically, I'm trying to get a user to input a series of numbers by keypad. Then when an operator button is pressed, the number entered is saved, and then the text field is cleared. But I have a problem, for some reason the String a = new String (aTextField1.getText()); doesn't seem to be called when i try to run the String b = new String (aTextField1.getText() + a);. Here's what i get:

Quote:
---------- Java Compiler ----------
GilGrissom.java:231: cannot resolve symbol
symbol: variable a
String b = new String (aTextField1.getText() + a);
^
1 error

Output completed (1 sec consumed) - Normal Termination
**Ran with an orchestrating class**

To make it a little clearer, I'll describe in steps my main purpose

-User enters (for example)"123" into the text field
-User clicks an operator, this then takes the number in the text field, stores it as a string, and then clears the area
-User enters (for example)"123" into the blank area
-User hits the equals button, which the if statement will check what operator has been pressed, and then adds the two strings together and outputs the result.

I'm a I on the right track at all? If not, please help. I've been racking my brain all day. Thanks to all those that can help..
Java-Head is offline   Reply With Quote
Old Mar 30th, 2006, 5:08 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
a is not defined in the same scope as the statement that uses it. To use a, you must define it in the same function, or globally in the class.
OpenLoop is offline   Reply With Quote
Old Mar 30th, 2006, 6:08 PM   #3
Java-Head
Newbie
 
Join Date: Mar 2006
Posts: 4
Rep Power: 0 Java-Head is on a distinguished road
Quote:
Originally Posted by OpenLoop
a is not defined in the same scope as the statement that uses it. To use a, you must define it in the same function, or globally in the class.
Thanks, so how would I define it in the global class. right at the top of the coding with the other JButton declerations?

Could you suggest a way I can define it in the same function?
Java-Head is offline   Reply With Quote
Old Mar 30th, 2006, 10:43 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 930
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by Java-Head
Thanks, so how would I define it in the global class. right at the top of the coding with the other JButton declerations?
Yes, that would work, and would probably be the best way to deal with it.

Quote:
Originally Posted by Java-Head
Could you suggest a way I can define it in the same function?
You need to understand the scope of the variable "a", or the context in which it is defined. In the code below,

1. if (add.equals(add))
2. {
3.     String a = new String (aTextField1.getText());
4.     aTextField1.setText("");
5. }
The variable "a" is defined on line 3. As soon as the compiler encounters a closing brace within which a variable is defined, it is destroyed*. So in your code, when the compiler reaches line 5, any variable(s) defined within the previous block of code, including "a", are destroyed. When you try to use the variable "a" in another function, it no longer exists and will cause the compiler to complain. You can solve this problem by defining the variable outside that code block, as you figured out; alternatively, if it is not holding a value that you are interested in, you could define it within the other fuction (although it will be created and destroyed each time the function is called).

* Actually, the variable "a" no longer references its previous value, in this case a String object, which is deleted when garbage collection occurs at some point in the future.
titaniumdecoy is online now   Reply With Quote
Old Apr 28th, 2006, 8:12 AM   #5
Java-Head
Newbie
 
Join Date: Mar 2006
Posts: 4
Rep Power: 0 Java-Head is on a distinguished road
Hi, I sorted out all the problems above thanks to the thread, but I have come up with a new problem. Basically, on the equals button I'm trying to tell it to either add, multiply, divide or minus.

So say if the user presses multiply the clocker will then equal 3..So when the equals button is pressed, it will look through the if statements till it gets to 3 and then do the required sum

**at the very top of my code*

int clocker = 0;

					aButton1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
					firstInput = Integer.parseInt(aTextField1.getText());
					clocker = 1;
					aTextField1.setText("")
			;}
		});	
			aButton2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
					firstInput = Integer.parseInt(aTextField1.getText());
					clocker = 2;
					aTextField1.setText("")
				
			;}
		});
			aButton3.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
					firstInput = Integer.parseInt(aTextField1.getText());
					clocker = 3;
					aTextField1.setText("")
				;}
		});
			aButton4.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
					firstInput = Integer.parseInt(aTextField1.getText());
					clocker = 4;
					aTextField1.setText("")
				;}
		});
	
			clear.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
				aTextField1.setText("");
				zero.setPreferredSize(new Dimension(x, y));
			}
		});
		equals.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent c)
			{
			secondInput = Integer.parseInt(aTextField1.getText());	

			if (clocker == 1)
			{
			sum = firstInput + secondInput;
			String hi = new String (firstInput + "+" + secondInput + "=" + sum);
			aTextField1.setText(hi);
			}

			 if (clocker == 2);
			{
			sum = firstInput -  secondInput;
			String hi = new String (firstInput + "+" + secondInput + "=" + sum);
			aTextField1.setText(hi);
			}
		}
	});
	}
}

But I keep on getting the folowing error of sum..



I know what a boolean is, but I thought that it didn't need to be used here. If it does, could anyone point me out on how to do it depending what operator buttin was pressed

Thanks

Last edited by Java-Head; Apr 28th, 2006 at 8:32 AM.
Java-Head is offline   Reply With Quote
Old Apr 28th, 2006, 11:54 AM   #6
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
			 if (clocker == 2);
			{
			sum = firstInput -  secondInput;
			String hi = new String (firstInput + "+" + secondInput + "=" + sum);
			aTextField1.setText(hi);
			}
If clocker was 2, then this code would subtract the two values, but display the expression as a plus. Maybe that is what is happening.
The Dark is offline   Reply With Quote
Old Mar 30th, 2006, 6:17 PM   #7
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
I guess that would do, i'm not very familiar with java.
OpenLoop 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:41 AM.

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