Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Calculator problem

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Calculator problem

    Hi, I'm having a slight problem with a JPanel calculator. I have no idea what the source of the problem is, but it seems to be doing operations very strangely. For example, if you press the buttons in this order: 3, x, 3, =, -, 6, = You would think the output would be 3 but actually it's 27

    I really don't know where the problem is so here is the full runnable code:
    package JFrameClaculator;
     
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
     
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SpringLayout;
     
    import layout.testing2;
     
    public class Calculator extends JPanel implements ActionListener{
     
    	public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    	static String textop;
    	static String textop1;
    	static int x;
    	static int x1;
    	static int x2;
    	static int x3;
    	public static final char LF = '\n';
    	public static final char CR = '\r';
    	static JTextField textField;
    	protected static JButton b1;
    	protected static JButton b2;
    	protected static JButton b3;
    	protected static JButton b4;
    	protected static JButton b5;
    	protected static JButton b6;
    	protected static JButton b7;
    	protected static JButton b8;
    	protected static JButton b9;
    	protected static JButton b10;
    	protected static JButton b11;
    	protected static JButton b12;
    	protected static JButton b13;
    	protected static JButton b14;
    	protected static JButton b15;
    	protected static JButton b16;
    	private static void createAndShowGUI() {
    		JFrame frame = new JFrame("Calculator v2.1");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container contentPane = frame.getContentPane();
            SpringLayout layout = new SpringLayout();
            contentPane.setLayout(layout);
            textField = new JTextField("", 17);
            textField.setEditable(false);
            b1 = new JButton("1");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            b1.setHorizontalTextPosition(AbstractButton.LEADING);
            b1.addActionListener(new Calculator());
            b1.setActionCommand("1");
            b2 = new JButton("2");
            b2.setVerticalTextPosition(AbstractButton.CENTER);
            b2.setHorizontalTextPosition(AbstractButton.LEADING);
            b2.addActionListener(new Calculator());
            b2.setActionCommand("2");
            b3 = new JButton("3");
            b3.setVerticalTextPosition(AbstractButton.CENTER);
            b3.setHorizontalTextPosition(AbstractButton.LEADING);
            b3.addActionListener(new Calculator());
            b3.setActionCommand("3");
            b4 = new JButton("4");
            b4.setVerticalTextPosition(AbstractButton.CENTER);
            b4.setHorizontalTextPosition(AbstractButton.LEADING);
            b4.addActionListener(new Calculator());
            b4.setActionCommand("4");
            b5 = new JButton("5");
            b5.setVerticalTextPosition(AbstractButton.CENTER);
            b5.setHorizontalTextPosition(AbstractButton.LEADING);
            b5.addActionListener(new Calculator());
            b5.setActionCommand("5");
            b6 = new JButton("6");
            b6.setVerticalTextPosition(AbstractButton.CENTER);
            b6.setHorizontalTextPosition(AbstractButton.LEADING);
            b6.addActionListener(new Calculator());
            b6.setActionCommand("6");
            b7 = new JButton("7");
            b7.setVerticalTextPosition(AbstractButton.CENTER);
            b7.setHorizontalTextPosition(AbstractButton.LEADING);
            b7.addActionListener(new Calculator());
            b7.setActionCommand("7");
            b8 = new JButton("8");
            b8.setVerticalTextPosition(AbstractButton.CENTER);
            b8.setHorizontalTextPosition(AbstractButton.LEADING);
            b8.addActionListener(new Calculator());
            b8.setActionCommand("8");
            b9 = new JButton("9");
            b9.setVerticalTextPosition(AbstractButton.CENTER);
            b9.setHorizontalTextPosition(AbstractButton.LEADING);
            b9.addActionListener(new Calculator());
            b9.setActionCommand("9");
            b10 = new JButton("0");
            b10.setVerticalTextPosition(AbstractButton.CENTER);
            b10.setHorizontalTextPosition(AbstractButton.LEADING);
            b10.addActionListener(new Calculator());
            b10.setActionCommand("0");
            b11 = new JButton("+");
            b11.setVerticalTextPosition(AbstractButton.CENTER);
            b11.setHorizontalTextPosition(AbstractButton.LEADING);
            b11.addActionListener(new Calculator());
            b11.setActionCommand("+");
            b12 = new JButton("-");
            b12.setVerticalTextPosition(AbstractButton.CENTER);
            b12.setHorizontalTextPosition(AbstractButton.LEADING);
            b12.addActionListener(new Calculator());
            b12.setActionCommand("-");
            b13 = new JButton("x");
            b13.setVerticalTextPosition(AbstractButton.CENTER);
            b13.setHorizontalTextPosition(AbstractButton.LEADING);
            b13.addActionListener(new Calculator());
            b13.setActionCommand("x");
            b14 = new JButton("/");
            b14.setVerticalTextPosition(AbstractButton.CENTER);
            b14.setHorizontalTextPosition(AbstractButton.LEADING);
            b14.addActionListener(new Calculator());
            b14.setActionCommand("/");
            b15 = new JButton("C");
            b15.setVerticalTextPosition(AbstractButton.CENTER);
            b15.setHorizontalTextPosition(AbstractButton.LEADING);
            b15.addActionListener(new Calculator());
            b15.setActionCommand("C");
            b16 = new JButton("=");
            b16.setVerticalTextPosition(AbstractButton.CENTER);
            b16.setHorizontalTextPosition(AbstractButton.LEADING);
            b16.addActionListener(new Calculator());
            b16.setActionCommand("=");
            contentPane.add(textField);
            contentPane.add(b1);
            contentPane.add(b2);
            contentPane.add(b3);
            contentPane.add(b4);
            contentPane.add(b5);
            contentPane.add(b6);
            contentPane.add(b7);
            contentPane.add(b8);
            contentPane.add(b9);
            contentPane.add(b10);
            contentPane.add(b11);
            contentPane.add(b12);
            contentPane.add(b13);
            contentPane.add(b14);
            contentPane.add(b15);
            contentPane.add(b16);
            b1.setPreferredSize(new Dimension(45, 30));
            b2.setPreferredSize(new Dimension(45, 30));
            b3.setPreferredSize(new Dimension(45, 30));
            b4.setPreferredSize(new Dimension(45, 30));
            b5.setPreferredSize(new Dimension(45, 30));
            b6.setPreferredSize(new Dimension(45, 30));
            b7.setPreferredSize(new Dimension(45, 30));
            b8.setPreferredSize(new Dimension(45, 30));
            b9.setPreferredSize(new Dimension(45, 30));
            b10.setPreferredSize(new Dimension(45, 30));
            b11.setPreferredSize(new Dimension(45, 30));
            b12.setPreferredSize(new Dimension(45, 30));
            b13.setPreferredSize(new Dimension(45, 30));
            b14.setPreferredSize(new Dimension(45, 30));
            b15.setPreferredSize(new Dimension(95, 30));
            b16.setPreferredSize(new Dimension(195, 30));
            //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, textField,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, textField,
                                 10,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b1,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b1,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
            //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b2,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b2,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b3,
                                 130,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b3,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b4,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b4,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b5,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b5,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b6,
                                 130,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b6,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b7,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b7,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b8,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b8,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b9,
                                 130,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b9,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b10,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b10,
                                 145,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b11,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b11,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b12,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b12,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b13,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b13,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b14,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b14,
                                 145,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b15,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b15,
                                 145,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b16,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b16,
                                 180,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
            frame.pack();
            frame.setSize(263, 259);
            frame.setVisible(true);
    	}
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource()==b1) {
    			String text = textField.getText();
    			textField.setText(text+"1");
    		}
    		if (e.getSource()==b2) {
    			String text = textField.getText();
    			textField.setText(text+"2");
    		}
    		if (e.getSource()==b3) {
    			String text = textField.getText();
    			textField.setText(text+"3");
    		}
    		if (e.getSource()==b4) {
    			String text = textField.getText();
    			textField.setText(text+"4");
    		}
    		if (e.getSource()==b5) {
    			String text = textField.getText();
    			textField.setText(text+"5");
    		}
    		if (e.getSource()==b6) {
    			String text = textField.getText();
    			textField.setText(text+"6");
    		}
    		if (e.getSource()==b7) {
    			String text = textField.getText();
    			textField.setText(text+"7");
    		}
    		if (e.getSource()==b8) {
    			String text = textField.getText();
    			textField.setText(text+"8");
    		}
    		if (e.getSource()==b9) {
    			String text = textField.getText();
    			textField.setText(text+"9");
    		}
    		if (e.getSource()==b10) {
    			String text = textField.getText();
    			textField.setText(text+"0");
    		}
    		if (e.getSource()==b11) {
    			textop = textField.getText();
    			textField.setText("");
    			x = x + 1;
    		}
    		if (e.getSource()==b12) {
    			textop = textField.getText();
    			textField.setText("");
    			x1 = x1 + 1;
    		}
    		if (e.getSource()==b13) {
    			textop = textField.getText();
    			textField.setText("");
    			x2 = x2 + 1;
    		}
    		if (e.getSource()==b14) {
    			textop = textField.getText();
    			textField.setText("");
    			x3 = x3 + 1;
    		}
    		if (e.getSource()==b15) {
    			String str = textField.getText();
    			if (str == null) {
     
    		      }
    		      int strLen = str.length();
    		      if(strLen != 0){
    		      if (strLen < 2) {
    		          textField.setText("");
    		      }
    		      int lastIdx = strLen - 1;
    		      String ret = str.substring(0, lastIdx);
    		      char last = str.charAt(lastIdx);
    		      if (last == LF) {
    		          if (ret.charAt(lastIdx - 1) == CR) {
    		              textField.setText(ret.substring(0, lastIdx - 1));
    		          }
    		      }
    		      textField.setText(ret);
    			}
    		}
    		if (e.getSource()==b16) {
    			if(x % 2 != 0)
    			{
    				textop1 = textField.getText();
    				int op = Integer.parseInt(textop);
    				int op1 = Integer.parseInt(textop1);
    				int opFinal = op+op1;
    				String Final = Integer.toString(opFinal);
    				textField.setText(Final);
    			}
    			if(x1 % 2 != 0)
    			{
    				textop1 = textField.getText();
    				int op = Integer.parseInt(textop);
    				int op1 = Integer.parseInt(textop1);
    				int opFinal = op-op1;
    				String Final = Integer.toString(opFinal);
    				textField.setText(Final);
    			}
    			if(x2 % 2 != 0)
    			{
    				textop1 = textField.getText();
    				int op = Integer.parseInt(textop);
    				int op1 = Integer.parseInt(textop1);
    				int opFinal = op*op1;
    				String Final = Integer.toString(opFinal);
    				textField.setText(Final);
    			}
    			if(x3 % 2 != 0)
    			{
    				textop1 = textField.getText();
    				int op = Integer.parseInt(textop);
    				int op1 = Integer.parseInt(textop1);
    				int opFinal = op/op1;
    				String Final = Integer.toString(opFinal);
    				textField.setText(Final);
    			}
    			else
    			{
     
    			}
    		}
    	}
     
    }

    I'm handing this in quite soon so any quick help would be greatly appreciated. Thanks!
    Last edited by The_Mexican; December 4th, 2010 at 10:02 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator problem

    textField.setText(text+"3")

    I doubt that's adding anything.

    All it appears to do is append a 3 to the String text.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator problem

    Oh....wait, that's what it's supposed to do.

    But I noticed that the word "Final" was highlighted as a keyword or as a class.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator problem

    On a side note, you could just define all your buttons in a constructor.

    But if you do that, they probably shouldn't be static anymore.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator problem

    And you'd have to have

    button.addActionListener(this);

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator problem

    I'm pretty sure your error is in this part
    public void actionPerformed(ActionEvent e) {
            if (e.getSource()==b1) {
                String text = textField.getText();
                textField.setText(text+"1");
            }
            if (e.getSource()==b2) {
                String text = textField.getText();
                textField.setText(text+"2");
            }
            if (e.getSource()==b3) {
                String text = textField.getText();
                textField.setText(text+"3");
            }
            if (e.getSource()==b4) {
                String text = textField.getText();
                textField.setText(text+"4");
            }
            if (e.getSource()==b5) {
                String text = textField.getText();
                textField.setText(text+"5");
            }
            if (e.getSource()==b6) {
                String text = textField.getText();
                textField.setText(text+"6");
            }
            if (e.getSource()==b7) {
                String text = textField.getText();
                textField.setText(text+"7");
            }
            if (e.getSource()==b8) {
                String text = textField.getText();
                textField.setText(text+"8");
            }
            if (e.getSource()==b9) {
                String text = textField.getText();
                textField.setText(text+"9");
            }
            if (e.getSource()==b10) {
                String text = textField.getText();
                textField.setText(text+"0");
            }
            if (e.getSource()==b11) {
                textop = textField.getText();
                textField.setText("");
                x = x + 1;
            }
            if (e.getSource()==b12) {
                textop = textField.getText();
                textField.setText("");
                x1 = x1 + 1;
            }
            if (e.getSource()==b13) {
                textop = textField.getText();
                textField.setText("");
                x2 = x2 + 1;
            }
            if (e.getSource()==b14) {
                textop = textField.getText();
                textField.setText("");
                x3 = x3 + 1;
            }
            if (e.getSource()==b15) {
                String str = textField.getText();
                if (str == null) {
     
                  }
                  int strLen = str.length();
                  if(strLen != 0){
                  if (strLen < 2) {
                      textField.setText("");
                  }
                  int lastIdx = strLen - 1;
                  String ret = str.substring(0, lastIdx);
                  char last = str.charAt(lastIdx);
                  if (last == LF) {
                      if (ret.charAt(lastIdx - 1) == CR) {
                          textField.setText(ret.substring(0, lastIdx - 1));
                      }
                  }
                  textField.setText(ret);
                }
            }
            if (e.getSource()==b16) {
                if(x % 2 != 0)
                {
                    textop1 = textField.getText();
                    int op = Integer.parseInt(textop);
                    int op1 = Integer.parseInt(textop1);
                    int opFinal = op+op1;
                    String Final = Integer.toString(opFinal);
                    textField.setText(Final);
                }
                if(x1 % 2 != 0)
                {
                    textop1 = textField.getText();
                    int op = Integer.parseInt(textop);
                    int op1 = Integer.parseInt(textop1);
                    int opFinal = op-op1;
                    String Final = Integer.toString(opFinal);
                    textField.setText(Final);
                }
                if(x2 % 2 != 0)
                {
                    textop1 = textField.getText();
                    int op = Integer.parseInt(textop);
                    int op1 = Integer.parseInt(textop1);
                    int opFinal = op*op1;
                    String Final = Integer.toString(opFinal);
                    textField.setText(Final);
                }
                if(x3 % 2 != 0)
                {
                    textop1 = textField.getText();
                    int op = Integer.parseInt(textop);
                    int op1 = Integer.parseInt(textop1);
                    int opFinal = op/op1;
                    String Final = Integer.toString(opFinal);
                    textField.setText(Final);
                }
                else
                {
     
                }
            }

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator problem

    x = x + 1;

    What is this doing?

    How is that value being saved?

    I can't find where x was initialized.

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Calculator problem

    This is where a debugger (or println statements) comes in very handy so one can step through the logic (as does posting a short compilable code snipped as you have). I'm not fully sure about the logic and your x,x1,x2...variables, but the math at that point leads to both
    if(x1 % 2 != 0)
    and
    if(x2 % 2 != 0)
    being entered, the result is the second value being evaluated and used (which is 27).

  9. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (December 4th, 2010)

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator problem

    How about this for times


    int whatever = Integer.parseInt(textField.getText());

    textField.setText(" ");

    // now here's where I'm not sure what'll happen

    int whatever2 = Integer.parseInt(textField.getText());

    int whatever3 = whatever * whatever2;

  11. #10
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Calculator problem

    Quote Originally Posted by copeg View Post
    This is where a debugger (or println statements) comes in very handy so one can step through the logic (as does posting a short compilable code snipped as you have). I'm not fully sure about the logic and your x,x1,x2...variables, but the math at that point leads to both
    if(x1 % 2 != 0)
    and
    if(x2 % 2 != 0)
    being entered, the result is the second value being evaluated and used (which is 27).
    I see, so I need to make it so that only one of the x if statements are true at a time...

  12. #11
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Calculator problem

    Quote Originally Posted by javapenguin View Post
    How about this for times


    int whatever = Integer.parseInt(textField.getText());

    textField.setText(" ");

    // now here's where I'm not sure what'll happen

    int whatever2 = Integer.parseInt(textField.getText());

    int whatever3 = whatever * whatever2;
    All I'm doing is making the string which the user entered into an int so I can multiply it with the other string that he entered.

Similar Threads

  1. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  2. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM
  3. calculator GUI needs to compute
    By javanovice in forum AWT / Java Swing
    Replies: 3
    Last Post: May 4th, 2010, 02:16 PM
  4. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM