• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot cast from File to String  RSS feed

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Java code that iterates through files in a directory like this:

Inside that for loop I want to put the filename into a HashMap like this:

The java compiler doesn't like my cast attempt: "Cannot cast from File to String"

How do I convert the filename into a String to put into my hash?

Thanks very much.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:How do I convert the filename into a String to put into my hash?


Well, your first step would be to get the filename from the File object. When you get it, it's going to be a String so you don't have to do any converting. What you need is a method from the File class which returns the filename.
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the hint.
 
Stan Lederer
Ranch Hand
Posts: 69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This worked.

Thanks, again, for the hint.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!