• Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting in file array  RSS feed

 
Kevin Carr
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a for loop that goes as long as there are files in a directory.

String dir = enterDirectory.getText();
File directory = new File(dir);
File[] files = directory.listFiles();
for(int i = 0; i < files.length(); i++) {
}

What exactly am I doing wrong? When I run it it returns an error. Is there anyway I could get it to retun all of the files in the directory as a string array instead of a file array?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Carr wrote:What exactly am I doing wrong? When I run it it returns an error.


What error? Which line? Please TellTheDetails.

Luck!
 
Kevin Carr
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I am trying to get teh total number of files but I don't know the right method because .length apparently is a different method in the io.file class. Says cannot find symbol, symbol : method length()
location: class java.io.file[]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Carr wrote:Basically I am trying to get teh total number of files but I don't know the right method because .length apparently is a different method in the io.file class. Says cannot find symbol, symbol : method length()
location: class java.io.file[]


It has nothing to do with methods of IO class. Your files variable is an array and there is no length() method for an array, but rather a length property. Simply use length without the parenthesis.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try use the for loop below instead
 
Michael Angstadt
Ranch Hand
Posts: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code, as it makes your code easier to read and the problem easier to solve.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!