About Question enthuware.ocpjp.v7.2.1211 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

About Question enthuware.ocpjp.v7.2.1211 :

Post by thchuong »

Hi,

In this link of java doc http://docs.oracle.com/javase/7/docs/ap ... .String%29
I don't understand why "*" has to be escaped in this line:
C:\\* Matches C:\foo and C:\bar on the Windows platform (note that the backslash is escaped; as a string literal in the Java Language the pattern would be "C:\\\\*")

why shouldn't it be C:\* and in java string "C:\\*"?

Thank you

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by admin »

You are not trying to escape *. You are trying to escape two backslashes i.e. \\.
If you like our products and services, please help us by posting your review here.

thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by thchuong »

Thank you for your answer.

OK now why we need two backslashes in this case?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by admin »

Because you are trying to match c:\foo not c:/foo. So you need to escape \ in the path matcher expression itself. So you get two \ in the expression. When you put it in java code, you need to escape each of them.
This explains more: http://www.jedit.org/users-guide/globs.html
http://mywiki.wooledge.org/glob
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by EelcoD »

Why isn't this discussion about the related question?

I don't understand the explanation: "Path objects that we are checking contain the complete path including directories (such as c:\works\pathtest\a.java and not just a.java). Therefore, nothing will match."

Why doesn't it match a.java
*.java would match everything that ends with .java, right?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by admin »

EelcoD wrote:Why isn't this discussion about the related question?
What do you mean? The question is about matching paths.
I don't understand the explanation: "Path objects that we are checking contain the complete path including directories (such as c:\works\pathtest\a.java and not just a.java). Therefore, nothing will match."

Why doesn't it match a.java
*.java would match everything that ends with .java, right?
Wrong. The * character matches zero or more characters of a name component without crossing directory boundaries.
Please go through this for complete details : http://docs.oracle.com/javase/7/docs/ap ... ng.String)

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by EelcoD »

But a.java resides in the pathtest directory, so you're not crossing any boundaries.
What am I missing?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by admin »

If you run the program and print each Path given to MyFileChecker by walkFileTree, you will see that the Paths are not relative paths. They all start with c:\works\pathtest. So it doesn't get a.java. It gets c:\works\pathtest\a.java. That is why the match fails.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by EelcoD »

So, the *.java will try to match it with c:\ or with works\ ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by admin »

EelcoD wrote:So, the *.java will try to match it with c:\ or with works\ ?
Not sure what you mean. I would suggest you to run the program and print the value of Path in the check method. Basically, *.java doesn't match c:\works\pathtest\a.java. *.java will only match a.java (which you are not getting in the check method.)
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by EelcoD »

It's hard for me to explain aparently..

The * doesnt cross boundaries meaning it doesnt check past the first backslash?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by admin »

I guess you can say that.
If you like our products and services, please help us by posting your review here.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by codecodecode67 »

Would "glob:*{.java}" and "glob:*.{java}" match all the java files just as "glob:**.java" would?

Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1211 :

Post by codecodecode67 »

yeah, they don't...we have to always match against the name of the file, not the whole path if we want one asterisk to work

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests