Results 1 to 6 of 6
  1. #1
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default pass "." as a string

    Hello, I am trying to build a calculator. To update the display I use the following code fired by a button

    if(e.getSource() == decimalb)
    updateDisplay(".");

    the upate function look like

    if(num1.length() < 21)
    {
    num1 += str;
    display.updateLabel(num1);
    }
    This works fine for all of the number button but the "." is not copied into the string. Therefore, I can't do decimals. any help would be appreciated.
    thanks
    bill

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Based on what you've posted, I am having difficulty guessing what isn't working. Sorry.

  3. #3
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default

    Hello Fubarable, I am building a calculator with buttons to enter numbers into a display. Each time a button is pressed, the digit is added to a string which holds the number that is displayed in a label. If num1 is equal to 12, and I press the decimalb button, it fires the updateDisplay(String str) method where str should be "." a period and this should be added to the end of the string. Well "." isn't making it to the updateDisplay method and updating num1 with a "." Here is the entire update function. Thanks for the help. I hope this is a little clearer.
    private void updateDisplay(String str)
    {
    if(firstNumber)
    {
    if(num1.length() < 21)
    {
    num1 += str;
    display.updateBottomLabel(num1);
    }
    else
    {
    Toolkit.getDefaultToolkit().beep();
    }
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You have a problem with your code, but you're not showing us the code that's causing the problem. Until you do, there's really no way anyone can help you. Also, please use code tags when posting code so that the code is readable. Good luck.

  5. #5
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default

    Yes you are right. I forgot to add an actionlistener for that button.
    thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    When you work with your ".", you'll want to make sure that you don't add the period twice to the display. To do this, check the text held by the display jtextfield and using String's contains method, see if it already has a period. If so, don't add a new one. I've used something like so:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class SimpleCalc {
      private static final String[][] BTN_STRINGS = {
        {"7", "8", "9", "/", "sqrt"},
        {"4", "5", "6", "*", "%"}, 
        {"1", "2", "3", "-", "1/x"},
        {"0", "+/-", ".", "+", "="}};
      private static final String NUMBER_BTN_STRINGS = "0123456789.";
      private JPanel mainPanel = new JPanel();
      private JTextField displayTField = new JTextField();
    
      public SimpleCalc() {
        JPanel buttonPanel = new JPanel(new GridLayout(0, BTN_STRINGS[0].length, 3, 3));
        
        NumberActionListener numberActionListener = new NumberActionListener();
        OperationActionListener operationActionListener = new OperationActionListener();
        for (int i = 0; i < BTN_STRINGS.length; i++) {
          for (int j = 0; j < BTN_STRINGS[i].length; j++) {
            String btnString = BTN_STRINGS[i][j];
            JButton button = new JButton(btnString);
            buttonPanel.add(button);
            
            // if button represents a number or period:
            if (NUMBER_BTN_STRINGS.contains(btnString)) {
              button.addActionListener(numberActionListener);
            } else {  // it's a math operation
              button.addActionListener(operationActionListener);
            }
          }
        }
        mainPanel.setBorder(new EmptyBorder(3, 3, 3, 3));
        mainPanel.setLayout(new BorderLayout(3, 3));
        mainPanel.add(displayTField, BorderLayout.NORTH);
        mainPanel.add(buttonPanel, BorderLayout.CENTER);
      }
    
      public JComponent getComponent() {
        return mainPanel;
      }
    
      private class NumberActionListener implements ActionListener {
    
        private static final int MAX_LENGTH = 15;
    
        public void actionPerformed(ActionEvent e) {
          String displayText = displayTField.getText();
          String buttonString = e.getActionCommand();
          
          if (displayText.length() >= MAX_LENGTH) {
            return;
          }
          
          // to avoid putting duplicate period in display text:
          if (!(buttonString.equals(".") && displayText.contains("."))) {
            displayText += buttonString;
            displayTField.setText(displayText);
          }
        }
      }
      
      private class OperationActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          // TODO: finish this
          
        }
      }
    
      private static void createAndShowUI() {
        JFrame frame = new JFrame("SimpleCalc");
        frame.getContentPane().add(new SimpleCalc().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  2. Replies: 3
    Last Post: 05-21-2009, 03:48 PM
  3. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 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
  •