hello...i m makin a calculator which inputs a expression n gives the result.
suppose i hv entered an expression( e.g. 12 + 3*5 -7) into an string variable .now if i want to check the string from starting index that it contains an integer or not ,then what is the idea...plezz help me out...i m a new user to java .
thank you in advance...!!

6 Years
Discussion Span
Last Post by jon.kiparsky

i want to check the string from starting index that it contains an integer

Define what you mean by an integer? How many integers does: 12 + 3*5 -7 contain?
I see 5: 12357

To test if a string contains an integer (define integer to be a digit char in the range 0-9): take the characters one at a time(See the String class for methods) and use a method from the Character class to determine if the character is a digit.


So you want to parse an infix expression? This can get complex. For example, the expression you've given, 12+3*5-7 has to be done as 12+ (3*5) - 7 for the correct result. Order of operations gets tricky.

But for your first step, you want to scan the input. The easiest method, conceptually, is to look at each character of the string in sequence. When you find an integer, you put it into an int variable and look at the next character. If the next character is also an int, it's part of the int you just scanned, so you have to figure out how to get it into that int in the right place. If it's not an int, you're done with that one, so you have to put that int somewhere where you can get at it when you're done scanning, and go back to step one.
This is not a complete algorithm, there are some steps that I've left pretty vague that you're going to have to figure out, but it's a start.

There are some tools that are easier to use, but you're probably best off writing this and learning the tricks when you know how to do it from the ground up.


no normR1...i want to scan the string as jon.kiparsky explained....

@kiparsky :thans..i got some idea ....first let me chek...then i tell u...thanxneway..

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.