• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

While loop question from SCJP 5.0

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From SCJP 5.0 (Sierra/Bates), pg 508 ques 1

Given:
import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}

Is the while expression correct? I thought no, it needs to evaluate to a boolean, which would be == not =. But according to the answers, I am wrong.

Can someone explain why?
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b is a boolean in this case. So even if = is an assignment and you need ==, it will evaluate to boolean because b is a boolean.

Hope this helps.
 
Higgledy Smith
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah, boolean is the ONLY type where = is not an assignment but a compare. I forgot. Thank you.
[ March 30, 2006: Message edited by: Higgledy Smith ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is correct because the Matcher find() method returns a boolean. The = is actually assigning the value true to the variable b when a match is found. This is similar to something like while(true) {} (but only when a match is found)

See here for more info on the Matcher class.
[ March 30, 2006: Message edited by: J Sato ]
 
Higgledy Smith
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another question on this question

source: ab34ef
012345 (index)
pattern: "\d*" (Looking for zero or more digits)

cmd-line: java Regex2 "\d*" ab34ef

output: 01234456
012 "3" 4 "4" 56

I broke the output into m.start() 012456
and m.group() "3" "4"

What happen to the 3 in m.start()? why did it just bump the index to 6?
 
J Sato
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also didn't quite understand the results...but if you use the reluctant (?) quantifier on the command line, you get the 3 back.

java Regex2 "\d*?" ab34efg
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too had this question and the response I got from this forum was that when you are using the 'greedy' quantifier, it reads from right to left. Thus, it consumes the 3 and 4 at the same time and skips the 3rd position (i.e. the 4 in the string to searched). I too am still somewhat confused as to why an additional index position shows up in the result (i.e. 6).
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A greedy quantifier forces a Matcher to digest the whole inputted string first. If the matching fails, it then forces the
Matcher to back off the inputted string by one character, check matching, and repeat the process until there are no more
characters left.
A reluctant quantifier, on the other hand, asks a Matcher to digest the first character of the whole inputted string first. If
the matching fails, it appends its successive character and checks again. It repeats the process until the Matcher digests the
whole inputted string.




I too am still somewhat confused as to why an additional index position shows up in the result (i.e. 6).


maybe you can find answer from java API



from above we can know that matcher will find until beyond the text.length .That is to say ,when Index is equal toinputString's length(),the matcher will continue to find
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the asterisk will match zero or more occurences of a digit. So even an no digit is a match. You will primairy use this for the possible occureence of sequence in between tokens. Like
where the pattern will even match if the input is without phonenumber (but with the quotes.)
Hope this helps.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic