About Question enthuware.ocpjp.v11.2.1822 :
Moderator: admin
-
- Posts: 5
- Joined: Sun Apr 04, 2021 2:19 pm
- Contact:
About Question enthuware.ocpjp.v11.2.1822 :
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.
-
- Posts: 5
- Joined: Sun Apr 04, 2021 2:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
JDK 1.8.0.211 return same result: "1 1 1 1"
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
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());
}
}
Print 6 3 9 9 on Java 8
Please post the exact code that you are trying out.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Apr 04, 2021 2:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
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());
}
}
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());
}
}
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Apr 04, 2021 2:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
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
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
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Apr 04, 2021 2:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
Thank you! I use MacOS and that's why I see incorrect answer. With separator "/" method return correct answer.
-
- Posts: 13
- Joined: Tue Jul 21, 2020 1:39 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
Can you help explain why getNameCount() returns 1 instead of 0 when the Path is empty?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.1822 :
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 120 guests