• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop not working  RSS feed

 
Basil Smart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I am seeing the string match in my array, but the if condition not picking up the match as seen below.



Any help will be greatly appreciated.

Thanks
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you getting "javaxt.io.Directory" from?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
How do you know that text is in the array in the first place? Try
System.out.println(Arrays.toString(files));
You do need Arrays.toString.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You are using a library extension which is based on the java.io library so I can only assume it works the same as the java.io classes but adds some extra functionality. The File equals() method compares an abstract pathname to the name the File object represents and so on some operating systems (eg Linux) case is important and different OS's use different file separator characters. Make sure all these are correct.

BTW the array contains File objects and not Strings. When you print a File object it outputs a string representation of it's abstract pathname.

 
Basil Smart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, thanks for your welcome and responses.

javaxt.io.Directory is an extension of the Java core library JavaXT Directory Search. They have some useful libraries there.

I see text in the array from line  as follows(C:\test C:\test\test.page C:\test\test2.page C:\test\test3.page). This code works in jsp to print output to browser, not the regular in java. I put the directly above the for loop no difference in output occurred.

Any other ideas, thanks?
 
Basil Smart
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys for your help, here's the solution below:

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you shouldn't call that a core library; it looks like a separate library provided by somebody else; it sounds useful however, so thanks to whoever supplies it.
That sort if thing looks as though it would be neater with a Stream. You can get a Stream object from an array with the corresponding method of the Arrays class (line 2). You can use the anyMatch() method which stops the Stream when it finds the first true value. Note that anyMatch returns a boolean. I shall let you read the Java™ Tutorials about the code in line 3 with -> which is called a λ expression.Note the slightly odd indentation for Streams: keep the lines short and make all the dots align vertically. You may get faster execution with a loop, but try reading the loop code and the Stream code out aloud, and you will (I hope) find the Stream easier to understand.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!