Reputation: 11
I have a java project for uni and this is my code. I have a problem with Length(). Can please anyone help me and spot m mistakes? Thank you in advance.
package mytest;
import java.io.File;
public class MyTest {
public static void main(String[] args) {
File here = new File("");
File[] contents = here.listFiles();
for (int counter = 0; counter < contents.Length(); counter++) {
System.out.println(contents[counter].getName());
}
File directory = here getParentDirectory();
System.out.println("The absolute path is" + directory.getAbsolutePath());
File[] list = directory.listFiles();
System.out.println("Directrory code contains" + list.Length() + "items\n.");
System.out.println("----\n");
for (int i = 0; i < list.Length(); i++) {
if (list[i].isDirectory()) {
System.out.println(list[i].getName() + "is a directory with" + list[i].listFiles.Length + "elements");
} else {
System.out.println(list[i].getName() + "has a size of" + list[i].Length() + "bytes.");
}
}
}
}
Upvotes: 0
Views: 74
Reputation: 50540
The issue is that you haven't read carefully what the documentation says:
Finally, you can use the built-in length property to determine the size of any array.
The proposed example is:
System.out.println(anArray.length);
In that case, length
is not a method, instead it's a built-in property of any array
. Because of that, trying to invoke it will result in an error.
Upvotes: 1
Reputation: 8387
For array should use
contents.length ;
But for string use this
contents.length();
Let me first highlight the different way for similar purpose.
length
--- arrays
(int[], double[], String[]
) ---- to know the length of the arrays.
length()
--- String
related Object (String, StringBuilde
r etc)to know the length of the String
Upvotes: 0
Reputation: 27812
When working on an array you need to use the .length field, there is no method Length(). You can replace in your code .Length() by .length and it should then be fine.
Be also aware however that on the last System.out statement, in the else block of the loop, the array is of type File[], hence each element is a File. A File has a method length() (lower care) which provides the length of the file as a long.
Moreover, in the if block of the loop, the File element of the array provides the method listFiles() (and not a field .listFiles) which is an array and as such, again, it has a field, length, giving you the length of the array.
We hence have:
Here is the fully compiling (fixed) code:
public static void main(String[] args) {
File here = new File("");
File[] contents = here.listFiles();
for (int counter = 0; counter < contents.length; counter++) {
System.out.println(contents[counter].getName());
}
File directory = here.getParentFile();
System.out.println("The absolute path is" + directory.getAbsolutePath());
File[] list = directory.listFiles();
System.out.println("Directrory code contains" + list.length + "items\n.");
System.out.println("----\n");
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) {
System.out.println(list[i].getName() + "is a directory with" + list[i].listFiles().length + "elements");
} else {
System.out.println(list[i].getName() + "has a size of" + list[i].length() + "bytes.");
}
}
}
Upvotes: 2
Reputation: 552
There is a big distinction between between the .Length(), a class method and the primitive type .length. While using strings, we use the .Length() as a class method. Since Strings are stored in classes in the background. To find the length of an array use the .length; as this actually searches inside the heap memory. If you like some more information on the background reason for this, I suggest you to check the following link.
http://www.programcreek.com/2013/04/what-does-a-java-array-look-like-in-memory/
Upvotes: 0