0

I was wondering how I would be able to account for spaces in my Calculator program. Right now it is set up to only work if there is a space after every value. I want it to be able to work if there is multiple spaces as well. I tried doing something (in my action performed, under the '=' sign test), but it didn't really work. So wanted to know how I could make it account for multiple spaces (like if there are more than one space before the next value, then it should recognize that that is the case, and delete the extra spaces). Thanks

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display = new JTextField(20);

doMath math = new doMath();
String s = "";
String b= "";
//int counter;

JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;

JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
JButton Space;
public GUI()
{
    super("Calculator");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout (2,1));

    buttonPanel = new JPanel();        
    buttonPanel.setLayout(new GridLayout(5, 4));
    buttonPanel.add(Num1 = new JButton("1"));
    buttonPanel.add(Num2 = new JButton("2"));
    buttonPanel.add(Num3 = new JButton("3"));
    buttonPanel.add(Num4 = new JButton("4"));
    buttonPanel.add(Num5 = new JButton("5"));
    buttonPanel.add(Num6 = new JButton("6"));
    buttonPanel.add(Num7 = new JButton("7"));
    buttonPanel.add(Num8 = new JButton("8"));
    buttonPanel.add(Num9 = new JButton("9"));
    buttonPanel.add(Num0 = new JButton("0"));        
    buttonPanel.add(Clr = new JButton("C"));
    buttonPanel.add(Eq = new JButton("="));
    buttonPanel.add(Add = new JButton("+"));
    buttonPanel.add(Sub = new JButton("-"));
    buttonPanel.add(Mult = new JButton("*"));
    buttonPanel.add(Div = new JButton("/"));
    buttonPanel.add(Space = new JButton("Space")); 

    Num1.addActionListener(this);
    Num2.addActionListener(this);
    Num3.addActionListener(this);
    Num4.addActionListener(this);
    Num5.addActionListener(this);
    Num6.addActionListener(this);
    Num7.addActionListener(this);
    Num8.addActionListener(this);
    Num9.addActionListener(this);
    Num0.addActionListener(this);
    Clr.addActionListener(this);
    Eq.addActionListener(this);
    Add.addActionListener(this);
    Sub.addActionListener(this);
    Mult.addActionListener(this);
    Div.addActionListener(this);
    Space.addActionListener(this);

    topPanel = new JPanel();         
    topPanel.setLayout(new FlowLayout());
    topPanel.add(display);

    add(mainPanel);

    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    setVisible(true);

}

public void actionPerformed(ActionEvent e) 
{
    JButton source = (JButton)e.getSource();
    String text = source.getText();
    int counter = 0;

    if (text.equals("=")) 
    {
        doMath math = new doMath();
        for(int i = 0; i <b.length(); i++)
        {

            String c = b.substring(i,(i+1));
            String d = b.substring((i+1),(i+2));
            String temp = "";
            if( c.equals(" ") && c.equals(d))
            {
                temp = b.substring(0,i)+(b.substring(i+1));
                b = temp;
            }
        }           
        int result = math.doMath1(b);

        String answer = ""+result;
        display.setText(answer);                   

    }
    else if(text.equals("Space"))
    {

        counter ++;
        if(counter <2)
        {            
            b+= " ";
            display.setText(b);
        }
        else 
        {
            b+="";
            display.setText(b);
        }
    }

    else if (text.equals("C"))
    {
        b = "";

        display.setText(b);
    }
    else 
    {

        b += (text);

        display.setText(b);

    }
    counter = 0; 

}
}
Big_Fan
  • 415
  • 2
  • 7
  • 20
  • I would tend to use the [`ScriptEngine`](http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html) for a calculator, as seen in [this answer](http://stackoverflow.com/a/6426684/418556) or in a nicer GUI as in [this answer](http://stackoverflow.com/a/7441804/418556). – Andrew Thompson Nov 11 '12 at 02:20

4 Answers4

1

This method requires knowledge of regular expressions. For each string, simply replace any instance of multiple consecutive spaces with a single space. In other words, using regular expressions, you would replace "\\s+", which means "more than one space," with " ", which is just the space character.

Example:

String c = ...
c = c.replaceAll("\\s+", " ");
Brandon
  • 1,336
  • 3
  • 10
  • 38
  • it is only working if there are two spaces. is there a way to make it keep on rechecking as long as there is a sequence of more than two spaces? – Big_Fan Nov 11 '12 at 02:36
1
String str = "your     string";
Pattern _pattern = Pattern.compile("\\s+");
Matcher matcher = _pattern.matcher(str);
str = matcher.replaceAll(" ");
sugandha sharma
  • 113
  • 1
  • 6
0

Instead of using a substring, use a Scanner which takes a String as a parameter.

Or you can disable the "Space" button after it's pressed, enabling it back, when a different button is pressed.

Mordechai
  • 15,437
  • 2
  • 41
  • 82
0

If b is not empty you could try looking at the previous character using charAt and if it is a space do nothing.

fabian
  • 111
  • 1
  • 10