Page 1 of 1

About Question enthuware.ocpjp.v8.2.1572 :

Posted: Fri Feb 02, 2018 11:22 am
by edmuns
Hi!

For my Ubuntu machine i've got this output:
c:\main\project\Starter.java

And there is no such answer or mentions about OS in the question.


However for this code from the book I've appropriate behavior of getName():

Code: Select all

Path path = Paths.get("/land/hippo/harry.happy");
System.out.println("The Path Name is: "+path);
for(int i=0; i<path.getNameCount(); i++) {
System.out.println("
Element "+i+" is: "+path.getName(i));
}
The Path Name is: /land/hippo/harry.happy
Element 0 is: land
Element 1 is: hippo
Element 2 is: harry.happy


Looks like my environment can't understand "\\", it works fine with "/" though. I'm using 1.8.0 openjdk-amd64.

Am I missing something or the test contains a mistake?

Re: About Question enthuware.ocpjp.v8.2.1572 :

Posted: Fri Feb 02, 2018 11:52 am
by admin
First thing is that you need to use the official Oracle's JDK while preparing fo the exam.
Second, if you see a path starting with c:\\, you can safely assume that it is a windows path and if the path contains / (i.e. forward slash), you can safely assume that it is a *nix path. So you test the code accordingly.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1572 :

Posted: Fri Feb 02, 2018 12:24 pm
by edmuns
For latest oracle jdk 8_162 I got same results.

Code: Select all

System.out.println(Paths.get("C:\\main\\project\\Starter.java").getName(0).toString());
Output:

c:\main\project\Starter.java

Re: About Question enthuware.ocpjp.v8.2.1572 :

Posted: Fri Feb 02, 2018 12:34 pm
by edmuns
I've used some online compilers and the result is same. Example:

https://codebunk.com/b/364194431/

Or you can try at your own this code:

Code: Select all

import java.nio.file.Path;
import java.nio.file.Paths;
class Main {
    public static void main(String[] args) {
             System.out.println(Paths.get("C:\\main\\project\\Starter.java").getName(0));
    }
}

Re: About Question enthuware.ocpjp.v8.2.1572 :

Posted: Fri Feb 02, 2018 10:04 pm
by admin
If you run it on *nix, then what you are getting is correct because backslash and colon are not path separators on *nix. Therefore, the whole name is considered as one single component.

That is why I mentioned the second point to you i.e. if you see a path starting with c:\\, you can safely assume that it is a windows path. In this case, it is a windows path and so if you run it on windows, you will get "main".

In the exam, you need to select the best option and option 5 i.e. main is the best option here.

HTH,
Paul.