Results 1 to 17 of 17
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default My GUI calculator, i need to have the "=" button work.

    hey, im creating my own GUI calculator (somewhat like windows's calculator).
    im building this interface with "windowbuilder pro8", and an absolute layout.
    i really got everything working, but not the "=" button.
    when i click the button with my current code, it gives me these errors:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "556+266"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at newcalc2$16.actionPerformed(newcalc2.java:173)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    my full code is:
    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class newcalc2 {
    	JFrame rekenmachine = new JFrame();
    	private JTextField textField;
    	
    	public newcalc2(){
    		
    		
    		
    		textField = new JTextField();
    		textField.setBounds(10, 11, 238, 25);
    		rekenmachine.getContentPane().add(textField);
    		textField.setColumns(10);
    		
    		final JButton btnNewButton = new JButton("7");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = btnNewButton.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		btnNewButton.setBounds(10, 61, 49, 35);
    		rekenmachine.getContentPane().add(btnNewButton);
    		
    		final JButton button = new JButton("4");
    		button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = button.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button.setBounds(10, 95, 49, 35);
    		rekenmachine.getContentPane().add(button);
    		
    		final JButton button_1 = new JButton("1");
    		button_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = button_1.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button_1.setBounds(10, 130, 49, 35);
    		rekenmachine.getContentPane().add(button_1);
    		
    		final JButton button_2 = new JButton("8");
    		button_2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = button_2.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button_2.setBounds(58, 61, 49, 35);
    		rekenmachine.getContentPane().add(button_2);
    		
    		final JButton button_3 = new JButton("5");
    		button_3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = button_3.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button_3.setBounds(58, 95, 49, 35);
    		rekenmachine.getContentPane().add(button_3);
    		
    		final JButton button_4 = new JButton("2");
    		button_4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String text = button_4.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button_4.setBounds(58, 130, 49, 35);
    		rekenmachine.getContentPane().add(button_4);
    		
    		final JButton button_5 = new JButton("9");
    		button_5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = button_5.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button_5.setBounds(106, 61, 49, 35);
    		rekenmachine.getContentPane().add(button_5);
    		
    		final JButton button_6 = new JButton("6");
    		button_6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = button_6.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button_6.setBounds(106, 95, 49, 35);
    		rekenmachine.getContentPane().add(button_6);
    		
    		final JButton button_7 = new JButton("3");
    		button_7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = button_7.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		button_7.setBounds(106, 130, 49, 35);
    		rekenmachine.getContentPane().add(button_7);
    		
    		final JButton btnNewButton_1 = new JButton("0");
    		btnNewButton_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = btnNewButton_1.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		btnNewButton_1.setBounds(10, 164, 97, 37);
    		rekenmachine.getContentPane().add(btnNewButton_1);
    		
    		final JButton btnNewButton_2 = new JButton(".");
    		btnNewButton_2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = btnNewButton_2.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		btnNewButton_2.setBounds(106, 164, 49, 37);
    		rekenmachine.getContentPane().add(btnNewButton_2);
    		
    		final JButton btnNewButton_3 = new JButton("/");
    		btnNewButton_3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = btnNewButton_3.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		btnNewButton_3.setBounds(153, 61, 49, 35);
    		rekenmachine.getContentPane().add(btnNewButton_3);
    		
    		final JButton btnNewButton_4 = new JButton("*");
    		btnNewButton_4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = btnNewButton_4.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		btnNewButton_4.setBounds(153, 95, 49, 35);
    		rekenmachine.getContentPane().add(btnNewButton_4);
    		
    		final JButton btnNewButton_5 = new JButton("+");
    		btnNewButton_5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = btnNewButton_5.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		btnNewButton_5.setBounds(153, 130, 49, 35);
    		rekenmachine.getContentPane().add(btnNewButton_5);
    		
    		final JButton btnNewButton_6 = new JButton("-");
    		btnNewButton_6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = btnNewButton_6.getText();
    				textField.setText(textField.getText()+text);
    			}
    		});
    		btnNewButton_6.setBounds(153, 164, 49, 37);
    		rekenmachine.getContentPane().add(btnNewButton_6);
    		
    		JButton btnNewButton_7 = new JButton("=");
    		btnNewButton_7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text = textField.getText();
    				int sum = Integer.parseInt(text);
    				System.out.println(sum);
    				textField.setText("Answer: "+sum);
    				textField.setEditable(false);
    			}
    		});
    		btnNewButton_7.setBounds(201, 130, 49, 71);
    		rekenmachine.getContentPane().add(btnNewButton_7);
    		
    		rekenmachine.setTitle("Rekenmachine");
    		rekenmachine.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Home\\Desktop\\calculator-icon.gif"));
    		rekenmachine.getContentPane().setLayout(null);
    		
    		JButton btnNewButton_8 = new JButton("CE");
    		btnNewButton_8.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				textField.setText("");
    				textField.setEditable(true);
    			}
    		});
    		btnNewButton_8.setBounds(201, 61, 49, 69);
    		rekenmachine.getContentPane().add(btnNewButton_8);
    		rekenmachine.setVisible(true);
    		rekenmachine.setSize(274,245);
    		
    	}
    }

    if any of you have a solution for this, that would be great!

    Thanks in advance.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: My GUI calculator, i need to have the "=" button work.

    You're going to have to parse the text. Java can't magically convert a String of characters to a mathematical expression- you have to do that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by KevinWorkman View Post
    You're going to have to parse the text. Java can't magically convert a String of characters to a mathematical expression- you have to do that.
    what do you mean with "parse the text"?
    what part of the text?

    i don't really understand what you mean.

  4. #4
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: My GUI calculator, i need to have the "=" button work.

    Well, the stack trace tells you everything - you're trying to make number of something that obviously is not a valid number. It even tells you on which line in your class the exception has occurred - line 173 - you try to parse that not-a-valid-number string.

    Now this is an example of the usage of parseInt:
    Java Code:
    int a = Integer.parseInt("34234234"); //34234234 - valid number
    int b = Integer.parseInt("342sdasds"); // not a valid number
    Note that this is only an example and it is working only with 2 operands(i.e. expressions like 4323+21312 and not like 3213+34234+12312312312)

    Have fun,
    Kalin

    P.S. @KevinWorkman - Point taken
    Last edited by kalata; 06-19-2013 at 05:21 PM.
    "One of the greatest ways to learn something is trying to teach it to someone else."

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: My GUI calculator, i need to have the "=" button work.

    Kalata, please don't post code solutions like that. That isn't helping. The whole point of programming assignments is to work through the problem, break it down into smaller steps, and tackle each step one by one. You've taken away the OP's ability to do that, which will actually hurt him in the long run.

    A better approach might have been to show him how to parse a String without doing all the work for him, or simply showing him the useful functions in the API.

    Spoonfeeding is not helping.

    Edit- Kalata took down the spoon-fed answer. Thanks.
    Last edited by KevinWorkman; 06-19-2013 at 05:22 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: My GUI calculator, i need to have the "=" button work.

    Maybe one of my blog articles can be of help: it describes and implements an expression parser and several evaluators; see the link in the top right of this reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by JosAH View Post
    Maybe one of my blog articles can be of help: it describes and implements an expression parser and several evaluators; see the link in the top right of this reply.

    kind regards,

    Jos
    sorry wrong post

  8. #8
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by JosAH View Post
    Maybe one of my blog articles can be of help: it describes and implements an expression parser and several evaluators; see the link in the top right of this reply.

    kind regards,

    Jos
    sure, ill do it

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: My GUI calculator, i need to have the "=" button work.

    I can understand not giving complete solutions. But considering the amount of work the OP put in on the program if this is spoon feeding then I am probably guilty of it too. I have also seen far more egregious examples than this from various moderators. It seems to be a very subjective area.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by jim829 View Post
    I can understand not giving complete solutions. But considering the amount of work the OP put in on the program if this is spoon feeding then I am probably guilty of it too. I have also seen far more egregious examples than this from various moderators. It seems to be a very subjective area.

    Regards,
    Jim
    well i read josAH's blog, but i haven't been able to figure this problem out yet.

  11. #11
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by kalata View Post
    Well, the stack trace tells you everything - you're trying to make number of something that obviously is not a valid number. It even tells you on which line in your class the exception has occurred - line 173 - you try to parse that not-a-valid-number string.

    Now this is an example of the usage of parseInt:
    Java Code:
    int a = Integer.parseInt("34234234"); //34234234 - valid number
    int b = Integer.parseInt("342sdasds"); // not a valid number
    Note that this is only an example and it is working only with 2 operands(i.e. expressions like 4323+21312 and not like 3213+34234+12312312312)

    Have fun,
    Kalin

    P.S. @KevinWorkman - Point taken
    lol wtf i parsed a string so many times, and it always worked.

    like:
    Java Code:
    Scanner input = new Scanner();
    String x = input.nextInt();
    int sum = Integer.parseInt(x);

  12. #12
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by MR bruto View Post
    lol wtf i parsed a string so many times, and it always worked.

    like:
    Java Code:
    Scanner input = new Scanner();
    String x = input.nextInt();
    int sum = Integer.parseInt(x);

    Scanner non args constructor doesn't exist. I don't know them well, but I think it is Scanner(System.in)
    And you are trying to convert a String into int -> String x = input.nextInt(); this is an error, I think it is just next()

  13. #13
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by superhaNds View Post
    Scanner non args constructor doesn't exist. I don't know them well, but I think it is Scanner(System.in)
    And you are trying to convert a String into int -> String x = input.nextInt(); this is an error, I think it is just next()
    lol yeah true, i forgot the "system.in".

    but no seriously, it works that way.

  14. #14
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: My GUI calculator, i need to have the "=" button work.

    It's impossible, it's like typing String s = 1;

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by MR bruto View Post
    lol wtf i parsed a string so many times, and it always worked.

    like:
    Java Code:
    Scanner input = new Scanner();
    String x = input.nextInt();
    int sum = Integer.parseInt(x);
    Yeah, like kalata pointed out, you can parse a String that contains a number using that method. But if the String contains things like a plus sign, you have to parse that yourself- there are methods in the String class for iterating through a String or even splitting on certain characters.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: My GUI calculator, i need to have the "=" button work.

    Quote Originally Posted by jim829 View Post
    I can understand not giving complete solutions. But considering the amount of work the OP put in on the program if this is spoon feeding then I am probably guilty of it too. I have also seen far more egregious examples than this from various moderators. It seems to be a very subjective area.

    Regards,
    Jim
    True, but the original post (before editing) contained a pretty cut and dry full code solution. He then edited it after I complained, and I have no problem with the post as it stands. But you're right about it being subjective.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: My GUI calculator, i need to have the "=" button work.

    Ah, I didn't realize that the edit had resulted in most of the code being removed. Now I understand and your post makes much more sense.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 2
    Last Post: 09-29-2012, 10:25 AM
  4. Default "X" button or EXIT_ON_CLOSE will not work
    By madroadbiker in forum New To Java
    Replies: 11
    Last Post: 03-21-2011, 04:35 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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