Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Beginning Java
How to print list of Files?
Gopu Akraju
Ranch Hand
Posts: 242
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have a list of Files. I would like to print their names. I tried as below:
But I get an exception as below: Can anyone tel me what the problem is?
private void createCheckBoxes(List javaFiles) { int count = javaFiles.size(); System.out.println("The number of files are"+count); Iterator it = javaFiles.iterator() ; while ( it.hasNext()){ String s = (String)it.next() ; System.out.println(s); }
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.io.File at extraction.Extract.createCheckBoxes(Extract.java:190) at extraction.Extract.actionPerformed(Extract.java:161) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:5488) at javax.swing.JComponent.processMouseEvent(JComponent.java:3126) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1778) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java
Ulf Dittmer
Rancher
Posts: 43081
77
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You can't cast a File to a
String
. Something like "System.out.println(((File) it.next).getName())" should do the trick.
Campbell Ritchie
Marshal
Posts: 80140
418
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Probably better however to use a parametrised Iterator.
. . .
Iterator<File> it = javaFiles.iterator();
. . .
System.out.println(it.next().getName());
That way you can dispense with the class casting.
Gopu Akraju
Ranch Hand
Posts: 242
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks for the tricky answer. I am able to print the name of the file.
But I have to store the
file name
in a
string
then start manupulating that. How do I do that? Thanks.
Ulf Dittmer
Rancher
Posts: 43081
77
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
"((File) it.next).getName()"
is
the file name as a string. What else are you looking for?
S Keith
Greenhorn
Posts: 4
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Heres some code that should explain things.
I've tried to keep it similar to the code you gave.
import java.util.*; import java.io.*; class Test{ public static void main(String [] args){ ArrayList<File> foo = new ArrayList<File>(); foo.add(new File("bob")); new Test().createCheckBoxes(foo); } private void createCheckBoxes(List<File> javaFiles) { int count = javaFiles.size(); Iterator<File> it = javaFiles.iterator() ; while ( it.hasNext()){ String s = (it.next()).getName() ; // use some string functions System.out.println(s + " in upper case is " + s.toUpperCase()); System.out.println(s + " is " + s.length() + " characters long"); } } }
running this gives:
C:\Users\Steven\Desktop>javac Test.java C:\Users\Steven\Desktop>java Test bob in upper case is BOB bob is 3 characters long C:\Users\Steven\Desktop>
http://java.sun.com/javase/6/docs/api/java/lang/String.html
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Interesting error with CipherInputStream
error handling
Java (blueJ)
how to use parseInt()
Please help with a simple GUI.
More...