help needed regarding Scanner class

hello

i'm really desperate for help, i'll mention that this is homework,but i need a small guidance.

i need to code a calculator,we need to use the Scanner class to break the expression to tokens(we CANNOT use other classes).

my main problem is about getting those tokens from the input string.

i tried breaking a string to tokens for hours but just couldnt do it.

a legal expression is something like:

xyzavbx1111+log(abc11*(2+5))+sin(4.5)

iilegal is : xx333aaaa111+2 or log()+2

where the alphanumerics are variables(sequence of letters followed by numbers)

i managed to get the tokens out of a string that looks like that:

String expression = "ax11111+32/3+4*2^2"

by doing:

_token = new Scanner(expression);

while(hasNext(_token))getNextToken(_token,"\\d+(\\.\\d+)?|(\\+)|(\\-)|(\\*)|(\\/)|(\\^)|([a-zA-Z]+(\\d+));

public static boolean hasNext(Scanner s) {

return s.hasNext();

}

public static String getNextToken(Scanner s,String str) {

return s.findInLine(str);

}

but i can't get the tokens when i have sin ,cos or log in the expression

either it just ignores it or i'm looping infinitely.

my question is how can i make the regular expression include the sin log and cos?

I also know that when i'll use the "matches" method ,i'll be able to spot illegal arguments like 5//2+ because it doesnt match the pattern.

so any help conecrning the regular expression would be great thanks alot.

[1549 byte] By [parallelya] at [2007-10-2 16:30:30]
# 1

Tokens, for your expressions, consist of types types: words and 'operators'.

Create an expression JUST for 'word'

Then create an expression JUST for 'operators'.

Put both in parenthesis.

Then or('|') them together.

Also use a character class, like '[a-z]' for your operators. It makes the expression more readable because you do not have to escape them.

jschella at 2007-7-13 17:33:00 > top of Java-index,Other Topics,Algorithms...
# 2

thx , but i already got it,after trying tons of combinations

i'll post the format i used in case someone will find this thread in the future ,while looking for something similar

the format is:

"(\\s)|(log)|(sin)|(cos)|(\\d+(\\.\\d+)?)|(\\+)"+

"|(\\-)|(\\*)|(\\/)|(\\^)|([a-zA-Z]+(\\d+))|(\\()|(\\))|(\\s)";

which includes white spaces between arguments and operators

parallelya at 2007-7-13 17:33:00 > top of Java-index,Other Topics,Algorithms...