Would you tell me what you want to know from your question ? You can see Sun's Java Tutorial for Pattern matching.
Jacob Sonia
Ranch Hand
Joined: Jun 28, 2009
Posts: 152
posted
0
Hi i feel you are confused about the output.
So here the pattern you give is 1 true 34 hi
so the first while loop executes for all 4 and it prints s for each, so this gives the output ssss
now next while loop starts
so first the output is i since the first input is 1
then it sees the next input is true a boolean, so the output is b
then it sees the next input is again an int, so the output is 34
lastly the input is hi, which is neither boolean nor int, so the last case is successful and the output is s2
so this gives ibis2
so the total output is ssssibis2
hope it is clear. The best thing to do now is try with different inputs and see if you getting what you predict
Asmita Khamkar
Greenhorn
Joined: Mar 28, 2009
Posts: 23
posted
0
Scanner class has the methods hasNext(), hasNextInt(), hasNextBoolean(). So in the first while loop it is only checking s1 i.e s1.hasNext() and concatenating s in hits variable every time this loop is run. In the next while loop it is checking s2 i.e. s2.hasNext() and within that loop it is checking s2.hasNextInt() or s2.hasNextBoolean() in if and is accordingly concatenating "i", "b" & "s2" in hits. Remember there are variables b and b2 declared as boolean but what you are concatenating in hits are strings "s", "b", "i" and not the values in variable. Try to understand the flow and I hope this clears your doubt.
Asmita
Faisal Ansari
Ranch Hand
Joined: Jun 23, 2009
Posts: 36
posted
0
thanks a lot ranchers for clearing my silly doubt
Faisal Ansari
Ranch Hand
Joined: Jun 23, 2009
Posts: 36
posted
0
here's another question
when it is invoked with the command line
java Regex2 "\d*" ab34ef
the output is
01234456
can anyone help me as to why the 4 is printed twice
This message was edited 1 time. Last update was at by Faisal Ansari
Please use one thread per question - so if you have a new question, just open a new topic. That way you'll have a better chance of getting your question answered, than when you hide it on the back of another topic.
il keep that in mind from the next time onwards....
can anyone help me with the answer to the above question
Parashuram Hallur
Ranch Hand
Joined: Sep 18, 2006
Posts: 41
posted
0
Faisal Ansari wrote:il keep that in mind from the next time onwards....
can anyone help me with the answer to the above question
Well it goes like this
Pattern is = \d* --> That means zero or more occurrences of the integer
input is = ab34ef
so the pattern matches with all characters and the index of the occurence is returned with the method start() and group() will return the matching string. Because of * a,b, e and f are matched. At the 2nd index 34 will be matched and it will be printed, since it has *, it will also match the index beside the matched string which is 4 here, so 34 is the matching string and the 4 is the matching location for nothing, hence 4 printed twice.
So it is like - (0) a (1) b (2) 34 (3) wont be considered as the string is consumed, thats why 3 is not printed in the output (4) for nothing (5) e (6) f...all other indexes matching except 2nd index ( 34 ) are for the "*", means it matches the empty.
HTH
This message was edited 2 times. Last update was at by Parashuram Hallur
Faisal Ansari
Ranch Hand
Joined: Jun 23, 2009
Posts: 36
posted
0
why isnt 3 printed twice...it is also an integer and is present at index 3
Regarding your confusion on the program (your first question), you know that the source, '1 true 34' or whatever, which is
used to construct the Scanner object, is split by Scanner at every SPACE by default--so there's not any matching per se[/]
going on, it's [i]tokenizing. And you can change the delimiter with the Scanner.useDelimiter() method, as you probably have read.
Then you work through the source with your Scanner object methods next() (gets the next token, and the tokens are all the
strings essentially that were separated by spaces in the source), nextInt() (gets the next int in the source and returns it AS AN
INT), etc.
Because you could get an exception if you call next(), nextInt() or other similar methods when there are no more tokens,
programs that use Scanner always make use of the hasNext() methods to make sure there is another token. The main
thing to remember about these is that the hasNext() method and hasNextInt() method and so on do not retrieve tokens
and so they do not advance the parser through the set of source tokens. That is, you could call hasNext() in an infinite
loop and never reach the end of the source--but if you called next(), or nextInt() in an infinite loop, you would definately
run out of tokens and get an exception.
I just thought that might have been what you were confused about.