I'm building a spreadsheet in Java.
I want to read a string and read the integers in it to do calculations.
Such as =2+(3*5)/4
Is there any method in string class to get integers.

8 Years
Discussion Span
Last Post by kaushalya_uom

I'm not sure if this would work, but you could find out the length of the String and use a loop with the charAt method in the String class. Then you could try to Typecast each char to type Integer. If no exceptions are thrown you know it is an Integer and you can go from there. Haven't tried it but just an idea that came to mind to attempt.

Hope it works out / helps.


Mostly likely he'll need to use Scanner or some regex parsing, because getting the numbers from the string is only the smallest part of writing an expression parser.

Edit: As far as trying the cast to Integer, it wouldn't work - you can't cast a char to an Integer object and it would be unnecessary, as the Character class has a static isDigit() method for that.


Thanx for ur quick replies!
Instead of reading integers I used StringTokenizer to divide the string in to substrings and do the appropriate action for the delimiter.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.