Hi!
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.

Recommended Answers

All 3 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.