0

I have a program, that whenever you type something and hit enter, it prints it out in a window (it's basically a command prompt). However, due to a few problems with a calculator function I'm trying to add, if the user input is a number, I want it to totally ignore it

    input = new JTextField();
    input.setEditable(true);
    input.setForeground(Color.WHITE);
    input.setCaretColor(Color.WHITE);
    input.setOpaque(false);

    input.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {

            String text = input.getText();

            if (text.length() >= 1){

                print(text + "\n", false);

                doCommand(text);
                scrollBottom();
                input.selectAll();
            }

        }
    });
  • you suppose to check last char of string whether it is `int`or not. – SatyaTNV Sep 30 '15 at 13:00
  • 1
    Welcome to StackOverflow! Please take some minute to review [how to ask good questions](http://stackoverflow.com/help/how-to-ask). Please note that this page is for asking specific questions about a specific problem. – mhutter Sep 30 '15 at 13:02
  • possible duplicate of [How to check if a String is a numeric type in Java](http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java) – Mage Xy Sep 30 '15 at 13:31

1 Answers1

0

You could check if text is a number with a regex like that:

text.matches("[1-9][0-9]*");

I hope it helps.

Leonid Glanz
  • 1,261
  • 2
  • 16
  • 36