Page 1 of 1

About Question enthuware.ocpjp.v11.2.1822 :

Posted: Sun Apr 04, 2021 2:35 pm
by mccar_
Hi! It looks like this code print "1 1 1 1". I double checked. I try this on online compiler (google first result "java online compiler") to eliminate my JDK influence.

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Sun Apr 04, 2021 2:46 pm
by mccar_
JDK 1.8.0.211 return same result: "1 1 1 1"

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Mon Apr 05, 2021 1:06 am
by admin

Code: Select all

class TestClass{
  public static void main(String args[]) throws Exception{
Path p1 = Paths.get("photos\\..\\beaches\\.\\calangute\\a.txt");
Path p2 = p1.normalize();
Path p3 = p1.relativize(p2);
Path p4 = p2.relativize(p1);

System.out.println(
    p1.getNameCount()+" "+p2.getNameCount()+" "+
    p3.getNameCount()+" "+p4.getNameCount());
  }
}
Prints 6 3 1 1 on Java 11 and 16

Print 6 3 9 9 on Java 8

Please post the exact code that you are trying out.

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Mon Apr 05, 2021 3:51 am
by mccar_
class TestClass{
public static void main(String args[]) throws Exception{
Path p1 = Paths.get("photos\\..\\beaches\\.\\calangute\\a.txt");
Path p2 = p1.normalize();
Path p3 = p1.relativize(p2);
Path p4 = p2.relativize(p1);

System.out.println(
p1.getNameCount()+" "+p2.getNameCount()+" "+
p3.getNameCount()+" "+p4.getNameCount());
}
}

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Mon Apr 05, 2021 5:18 am
by admin
Not sure why you are getting 1 1 1 1. I am getting the output as mentioned above. Are you sure you have the exact same code and running it from command line?

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Mon Apr 05, 2021 4:17 pm
by mccar_
I run your code in online compiler https://www.w3schools.com/java/tryjava. ... o_compiler and others to make suer that it is not my JDK issue. All results the same: 1 1 1 1.
To reproduce, please, copy code from your post into any online compiler and run it.
Online compilers list: https://www.baeldung.com/java-online-compilers

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Mon Apr 05, 2021 10:55 pm
by admin
oh, that's because the online compiler is using a unix based OS and on such OSs the path separator is /. Therefore, if you use "photos\\..\\beaches\\.\\calangute\\a.txt" on a unix based OS, it will be considered as a single name.

If you change the given path to "photos/../beaches/./calangute/a.txt" on Unix you will get the same result as given.

In the exam, if you see double backslash (\\), assume that it is a windows path and if you see a forward slash (/), assume that it is a unix path.

We use windows path because most people use Windows OS and so, windows path makes the code easier for them to test.

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Mon Apr 05, 2021 10:57 pm
by admin
Also, we do not recommend using online compilers to try out code because you won't know which jdk build is being used. Better to use official Oracle or OpenJDK on your machine through command line.

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Tue Apr 06, 2021 1:39 am
by mccar_
Thank you! I use MacOS and that's why I see incorrect answer. With separator "/" method return correct answer.

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Wed Apr 21, 2021 2:09 am
by 61d14837
Can you help explain why getNameCount() returns 1 instead of 0 when the Path is empty?

Re: About Question enthuware.ocpjp.v11.2.1822 :

Posted: Wed Apr 21, 2021 11:02 pm
by admin
That is how they have designed the Path class. Documentation for getNameCount says: "the number of elements in the path, or 0 if this path only represents a root component". So, the number of elements starts with 1, unless it is a root such as "c:\" on windows or "/" on unix.