New to Java

Hi,

everyone. I am new to Java and I have a big assignment due by next week. I have found one method of filtering file names. When I tried compiling the code, I got error message.

the code is:

--

from: http://home.cogeco.ca/~ve3ll/jatutor8.htm

public class OnlyExt implements FileNameFilter

{

String ext;

public OnlyExt(String ext)

{ this.ext = "." + ext; }

public boolean accept(File dir, String name)

{ return name.endsWith(ext); }

}

class DirListOnly

{

public static void main(String args[])

{

String dirname="./website";

File f1 = new File(dirname);

FileNameFilter only = new OnlyExt("xml");

String s[] = f1.list(only);

for (int i=0; i<s.length; i++)

{ System.out.println(s); }

}

}

--

The error message is:

C:\OnlyExt.java:3: cannot resolve symbol

symbol : class FileNameFilter

location: class OnlyExt

public class OnlyExt implements FileNameFilter

^

C:\OnlyExt.java:17: cannot resolve symbol

symbol : class FileNameFilter

location: class DirListOnly

FileNameFilter only = new OnlyExt("xml");

^

2 errors

Tool completed with exit code 1

If anybody can help me what I am doing wrong?

I changed the file name to be filtered from html --> xml. that's the only change i made.

thanks a lot

[1465 byte] By [learnJava75a] at [2007-10-2 21:07:46]
# 1

FileNameFilter is an interface from the java.io package, so you'll need to import that interface first.

import java.io.FileNameFilter;

public class OnlyExt implements FileNameFilter

{

// your code

}

Good luck.

prometheuzza at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks for your reply. It didn't work. I got again the same error messages.thanks
learnJava75a at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...
# 3

> Thanks for your reply.

> It didn't work. I got again the same error messages.

>

> thanks

Then you probably have a classpath issue.

Read this document carefully and follow the steps exactly:

Windows: http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

UNIX/Linux: http://java.sun.com/docs/books/tutorial/getStarted/cupojava/unix.html

Mac OS: http://java.sun.com/docs/books/tutorial/getStarted/cupojava/mac.html

prometheuzza at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...
# 4
prometheuzz is exactly right, but you both have the class name spelled incorrectly. If you look at the javadocs, you'll see that it's java.io.FilenameFilter, not java.io.FileNameFilter.Case matters in Java.%
duffymoa at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...
# 5

> prometheuzz is exactly right, but you both have the

> class name spelled incorrectly. If you look at the

> javadocs, you'll see that it's

> java.io.FilenameFilter, not java.io.FileNameFilter.

>

> Case matters in Java.

>

> %

Jeez, I'm on a role today! I also need to read the API-docs carefully...

; )

prometheuzza at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks a lot for all of you guys. It works. :))
learnJava75a at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...
# 7
s/role/roll/You ARE on a roll! 8)%
duffymoa at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...
# 8
> s/role/roll/> > You ARE on a roll! 8)> > %Well, at least I'm consistent in my spelling.; )
prometheuzza at 2007-7-13 23:53:29 > top of Java-index,Java Essentials,New To Java...