3

I am developing a programmable calculator in android.I am getting user input in the form of charsequence which i then convert into string. But before i can put them in the stack i must check whether the input is an integer or a sign.

I am basically from a C++ background. So i am having issues finding the right functions to do the job. Any help would be appreciated.

Paresh Mayani
  • 125,853
  • 70
  • 238
  • 294
Some Body
  • 1,781
  • 3
  • 18
  • 16
  • http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence – Oleg Mikheev Feb 14 '12 at 11:05
  • Thank you for the answers. Now i understand how to check whether the input is int or not. However in the next step when i determine that some input is not integer how can i check if that input is a +,-,* or / sign. – Some Body Feb 14 '12 at 11:29

4 Answers4

4

Two possible solutions:

  1. You can create a regex that checks if your string is a number
  2. You can try to extract its integer value using Integer.valueOf(), and catch the exception [NumberFormatException] thrown - if it is not a number.

The first is most likely to be more efficient then the second

amit
  • 172,148
  • 26
  • 225
  • 324
0

Don't allow the user to enter text and special characters in Textbox(Edittext).When user enters the number in the textbox convert that number to integer as follows Integer.parseInt(yourstring);

Venkata Krishna
  • 1,554
  • 1
  • 11
  • 19
0

You need to us Integer.parseInt and catch the appropriate NumberFormatException if the user does not enter a number:

try
{
  // the String to int conversion happens here
  int i = Integer.parseInt(s.trim());

  // print out the value after the conversion
  System.out.println("int i = " + i);
}
catch (NumberFormatException nfe)
{
  System.out.println("NumberFormatException: " + nfe.getMessage());
}
BeRecursive
  • 6,146
  • 1
  • 22
  • 40
0

Can match for the validaty of input with this regex through [+-]?\d*

manocha_ak
  • 904
  • 7
  • 19