Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 14th, 2013, 03:14 PM
lo22 lo22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 lo22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 47 sec
Reputation Power: 0
Why is this method not returning anything?

I have these two classes that make a simple calculator, however when I try to use it I get the error java.lang.NumberFormatException, which I get because the getText method in the SimpleCalc class is returning an empty String. Here is the source code:

Of the SimpleCalc class:
Code:
	//Imports are listed in full to show what's being used
	//could just import javax.swing.* and java.awt.* etc..

	import java.awt.GridLayout;
	import java.awt.BorderLayout;
	import java.awt.event.ActionListener;
	import java.awt.event.ActionEvent;
	import javax.swing.JFrame;
	import javax.swing.JPanel;
	import javax.swing.JTextField;
	import javax.swing.JButton;
	import java.awt.Container;

	public class SimpleCalc implements ActionListener{
	 
		public static final SimpleCalc instance = new SimpleCalc();
		
		JFrame guiFrame;
		JPanel buttonPanel;
		JTextField numberCalc;
		int calcOperation = 0;
		int currentCalc;
		int operatorAction;
		
		//Note: Typically the main method will be in a
		//separate class. As this is a simple one class
		//example it's all in the one class.
		
		
		public SimpleCalc()
		{
			guiFrame = new JFrame();
			
			//make sure the program exits when the frame closes
			guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			guiFrame.setTitle("Simple Calculator");
			guiFrame.setSize(300,300);
		  
			//This will center the JFrame in the middle of the screen
			guiFrame.setLocationRelativeTo(null);
			
			numberCalc = new JTextField();
			numberCalc.setHorizontalAlignment(JTextField.RIGHT);
			numberCalc.setEditable(false);
			
			guiFrame.add(numberCalc, BorderLayout.NORTH);
			
			buttonPanel = new JPanel();
				   
			//Make a Grid that has three rows and four columns
			buttonPanel.setLayout(new GridLayout(4,3));   
			guiFrame.add(buttonPanel, BorderLayout.CENTER);
			
			//Add the number buttons
			for (int i=1;i<10;i++)
			{
				addButton(buttonPanel, String.valueOf(i));
			}

			JButton addButton = new JButton("+");
			addButton.setActionCommand("+");
			
			OperatorAction subAction = new OperatorAction(1);
			addButton.addActionListener(subAction);
			
			JButton subButton = new JButton("-");
			subButton.setActionCommand("-");
			
			OperatorAction addAction = new OperatorAction(2);
			subButton.addActionListener(addAction);
			
			JButton equalsButton = new JButton("=");
			equalsButton.setActionCommand("=");
			equalsButton.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent event)
				{
					if (!numberCalc.getText().isEmpty())
					{
						int number = Integer.parseInt(numberCalc.getText()); 
						if (calcOperation == 1)
						{
							int calculate = currentCalc  + number;
							numberCalc.setText(Integer.toString(calculate));
						}
						else if (calcOperation == 2)
						{
							int calculate = currentCalc  - number;
							numberCalc.setText(Integer.toString(calculate));
						}
					}
				}
			});
			
			buttonPanel.add(addButton);
			buttonPanel.add(subButton);
			buttonPanel.add(equalsButton);
			guiFrame.setVisible(true);  
		}
		
		//All the buttons are following the same pattern
		//so create them all in one place.
		private void addButton(Container parent, String name)
		{
			JButton but = new JButton(name);
			but.setActionCommand(name);
			but.addActionListener(this);
			parent.add(but);
		}
		
		//As all the buttons are doing the same thing it's
		//easier to make the class implement the ActionListener
		//interface and control the button clicks from one place
		@Override
		public void actionPerformed(ActionEvent event)
		{
			//get the Action Command text from the button
			String action = event.getActionCommand();
			
			//set the text using the Action Command text
			numberCalc.setText(action);       
		}
		
		public String getText() {
			return numberCalc.getText();
		}
		
	}


And here is the other class OperatorAction:

Code:
import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;

	import javax.swing.JTextField;

		public class OperatorAction implements ActionListener
		{
			int calcOperation = 0;
			int currentCalc;
			private int operator;
			// How to make 
		  
			
			public OperatorAction(int operation)
			{
				operator = operation;
			}
			
			public void actionPerformed(ActionEvent event)
			{
				currentCalc =   Integer.parseInt(SimpleCalc.instance.getText()); 
				calcOperation = operator;
			}
		}


So I do get that it is my getText method that is failing, but why? I really cannot see why this should not work :/

And why is it that I cannot access the public JTextField of the SimpleCalc class, with it is inherited getText method. If I try to do that Eclipse says that it does not this variable... So should public fields not be accessible to other classes?

Reply With Quote
  #2  
Old February 15th, 2013, 05:49 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,045 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 44 m 17 sec
Reputation Power: 346
Where is the main() method for executing this?

Reply With Quote
  #3  
Old February 15th, 2013, 09:14 AM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,828 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 11 h 21 m 4 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
My suspicion is that you have nothing in the JTextField. When you use the getText() method, it returns "", which cannot be converted to a number. I would recommend initializing that field with "0".

I don't see a JTextField in that class that is declared public. There is only one instance variable declared public.

Reply With Quote
  #4  
Old February 15th, 2013, 09:48 AM
lo22 lo22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 lo22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 47 sec
Reputation Power: 0
Ok well firstly here is my main class:

Code:
import java.awt.EventQueue;


public class Main {

	public static void main(String[] args) {
		  
        //Use the event dispatch thread for Swing components
        EventQueue.invokeLater(new Runnable()
        {
            
           @Override
            public void run()
            {
              
                new SimpleCalc();         
            }
        });
             
   }
}


And well I get this GUI calculator which is shown here, and then I get this error when I try to use the "-" or "+" button, so it is not because the JTextField is actually empty.

Here is the picture:



Also found here: http://tinypic.com/r/2mevbyu/6

And lastly I do have a public field JTextField in my simpleCalc called numberCalc, why is it that cannot just use that with the .getText (inherited from JTextField) method in the OperatorAction?

And thanks for helping, it really means a lot!

Reply With Quote
  #5  
Old February 15th, 2013, 10:37 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,045 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 44 m 17 sec
Reputation Power: 346
How many instances of the SimpleCalc class are created? Which one does the getText() use to get its data from?

Reply With Quote
  #6  
Old February 15th, 2013, 11:12 AM
lo22 lo22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 lo22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
How many instances of the SimpleCalc class are created? Which one does the getText() use to get its data from?


One as its constructor makes sure it is a singleton via this code fragment:

Code:
public static final SimpleCalc instance = new SimpleCalc();

Reply With Quote
  #7  
Old February 15th, 2013, 11:23 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,045 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 44 m 17 sec
Reputation Power: 346
What about the one created in the main() method?

If you are not sure how many are being created, add a println() statement in the constructor and see how many times it prints out a message.

What does that statement have to do with the class being a singleton?

Reply With Quote
  #8  
Old February 15th, 2013, 01:04 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,828 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 11 h 21 m 4 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
I see the numberCalc variable, but it's not declared public. It has default access which means only members of the same package or members of the class can access it. If you want subclasses to access it that aren't in the package, declare it protected.

Reply With Quote
  #9  
Old February 15th, 2013, 02:13 PM
lo22 lo22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 lo22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 47 sec
Reputation Power: 0
Yeah well the main class creates two object of SimpleCalc, at the present state. So it seems what I have done so far does not really work.

But well what I actually just want to do, is find a way to transfer the content of the JTextField in the SimpleCalc, to a variable in the OperatorAction class, which is used to implement the various operator (+,-).

So how could I do this most easily?

Reply With Quote
  #10  
Old February 15th, 2013, 02:35 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,045 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 44 m 17 sec
Reputation Power: 346
Quote:
So how could I do this most easily?

Create only one instance of the class and use that one. Pass a reference to it to other classes that need to be able to access its members.

Reply With Quote
  #11  
Old February 16th, 2013, 06:40 AM
lo22 lo22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 lo22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Create only one instance of the class and use that one. Pass a reference to it to other classes that need to be able to access its members.


Yeah well but as you can see I have not really been able to do that. So could you perhaps give me an example of how to do this?

Reply With Quote
  #12  
Old February 16th, 2013, 06:47 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,045 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 44 m 17 sec
Reputation Power: 346
Get rid of the second call to the constructor so that there is only one instance created (in the main() method).
Make the action listener class an inner method so that it as access to the method.

Last edited by NormR : February 16th, 2013 at 06:50 AM.

Reply With Quote
  #13  
Old February 23rd, 2013, 03:57 PM
burakaltr burakaltr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 46 burakaltr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 58 m 48 sec
Reputation Power: 0
Code:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Digital extends JFrame{
public Digital(){
	;
	setVisible(true);
	add(new Panel());
	setSize(400,400);
	//t.setSize(20, 20);
	 //pack();
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String args[]){
	
	new Digital();
	
}
}
class Panel extends JPanel implements ActionListener{
	JButton c[]=new JButton[10];
	JTextField t=new JTextField(20);
	JButton mul=new JButton("*");
	JButton div=new JButton("/");
	JButton add=new JButton("+");
	JButton sub=new JButton("-");
	public Panel(){   
		//setLayout(null);

	for(int i=0;i<10;i++)c[i]=new JButton(String.valueOf(i));
//	setLayout(new GridLayout(3,3));
		//setSize(100,100);
		//setLayout(new GridLayout(3,3));
		
		for(int i=0;i<10;i++)add(c[i]);
		
		//setLayout(new BorderLayout());
		for(int i=0;i<10;i++)c[i].addActionListener(this);
		//setLayout(new BorderLayout());
add(mul);add(div);add(add);add(sub);
 mul.addActionListener(new ActionListener(){

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String d=t.getText();
		t.setText(d+String.valueOf("*"));
	}
	 
	 
	 
 });
 sub.addActionListener(new ActionListener(){

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String d=t.getText();
		t.setText(d+String.valueOf("-"));
	}
	 
	 
	 
 });
 div.addActionListener(new ActionListener(){

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String d=t.getText();
		t.setText(d+String.valueOf("/"));
	}
	 
	 
	 
 });
 add.addActionListener(new ActionListener(){

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String d=t.getText();
		t.setText(d+String.valueOf("+"));
	}
	 
	 
	 
 });
	//setLayout(new FlowLayout());

		add(t);
		
		
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

		for(int i=0;i<10;i++)if(arg0.getSource()==c[i]) {
			String d=t.getText();
			t.setText(d+String.valueOf(i));
		}
	}
	
}


Another calculator code could be as such ! Maybe you can adapt it t your case

Good Luck !

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Why is this method not returning anything?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap