Solved

Please help me to compile this Java Program !!!

Posted on 2004-08-11
5
751 Views
Last Modified: 2012-06-22
This Java Program when runs prompt users to enter a directory name and then the program lists the contents of the directory dir, including the contents of its subdirectories to any level of nesting through recursion.

However I'm having compilation errors. Heres the program

import java.lang.*;
import java.io.*;

public class RecuDirList {

     public static void main(String[] args) {

     String directoryName; // Directory name entered by the user.
     File directory; // File object referring to the directory.

     TextIO.putln("Enter a directory name: ");
     directoryName = TextIO.getln().trim();
     directory = new File(directoryName);

     if (directory.isDirectory() == false) {
            // Program needs a directory name. Print an error message.
            if (directory.exists() == false) {
      TextIO.putln();
                TextIO.putln("There is no such directory!"); }
            else
                TextIO.putln("That file is not a directory.");
     }
     else {
              // List the contents of directory, with no indentation
             // at the top level.
             listContents( directory, "" );
    }

} // end main()


static void listContents(File dir, String indent) {
        // A recursive subroutine that lists the contents of
        // the directory dir, including the contents of its
        // subdirectories to any level of nesting.         
                  String[] files; // List of names of files in the directory.
        TextIO.putln(indent + "Directory \"" + dir.getName() + "\":");
        indent += " "; // Increase the indentation for listing the contents.
        files = dir.list();
        for (int i = 0; i < files.length; i++)
        {
              // If the file is a directory, list its contents
              // recursively. Otherwise, just print its name.
             File f = new File(dir, files);
             if (f.isDirectory())
                  listContents(f, indent);
             else
               TextIO.putln(indent + files); }
  } // end listContents()


} // end class RecuDirList

____________________________________________________________________________

When I compile I get the following errors

C:\java>javac RecuDirList.java
RecuDirList.java:11: cannot resolve symbol
symbol  : variable TextIO
location: class RecuDirList
     TextIO.putln("Enter a directory name: ");
     ^
RecuDirList.java:12: cannot resolve symbol
symbol  : variable TextIO
location: class RecuDirList
     directoryName = TextIO.getln().trim();
                     ^
RecuDirList.java:18: cannot resolve symbol
symbol  : variable TextIO
location: class RecuDirList
                                TextIO.putln();
                                ^
RecuDirList.java:19: cannot resolve symbol
symbol  : variable TextIO
location: class RecuDirList
                TextIO.putln("There is no such directory!"); }
                ^
RecuDirList.java:21: cannot resolve symbol
symbol  : variable TextIO
location: class RecuDirList
                TextIO.putln("That file is not a directory.");
                ^
RecuDirList.java:41: cannot resolve symbol
symbol  : variable TextIO
location: class RecuDirList
          TextIO.putln(indent + "Directory \"" + dir.getName() + "\":");
          ^
RecuDirList.java:48: cannot resolve symbol
symbol  : constructor File  (java.io.File,java.lang.String[])
location: class java.io.File
                 File f = new File(dir, files);
                          ^
RecuDirList.java:52: cannot resolve symbol
symbol  : variable TextIO
location: class RecuDirList
                   TextIO.putln(indent + files); }
                   ^
8 errors

What is it that causing "cannot resolve symbol variable TextIO"...???

I am using Windows XP and jdk1.3.1_04

THANKS
0
Comment
Question by:nickmcdonald
  • 2
5 Comments
 

Author Comment

by:nickmcdonald
ID: 11776039
Please enlighten me with this compilation problem for this Java Program !!!!!
0
 
LVL 1

Accepted Solution

by:
schmida earned 100 total points
ID: 11776517
Hi nick, the problem is that you try to use a class (TextIO) your compiler can t find. Why do you use TextIO for input and output? Where did you get the code

TextIO.putln()

from? Is it from a book or from the web? Maybe you can download this class from there and include (import) it into your program. This would solve your compilation errors. Another possibility would be to use the java standard to output test which is

System.out.println()

Here is your code without compilation errors:



import java.io.*;


public class RecuDirList {

     public static void main(String[] args) {

     String directoryName = ""; // Directory name entered by the user.
     File directory; // File object referring to the directory.

     System.out.println("Enter a directory name: ");
     try
      {
            directoryName = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
      }
      catch (Exception e)
      {
            System.out.println("Error:" + e.getMessage());
      }
     
     directory = new File(directoryName);

     if (directory.isDirectory() == false) {
            // Program needs a directory name. Print an error message.
            if (directory.exists() == false) {
     System.out.println("");
                System.out.println("There is no such directory!"); }
            else
            System.out.println("That file is not a directory.");
     }
     else {
              // List the contents of directory, with no indentation
             // at the top level.
             listContents( directory, "" );
    }

} // end main()


static void listContents(File dir, String indent) {
       // A recursive subroutine that lists the contents of
       // the directory dir, including the contents of its
       // subdirectories to any level of nesting.        
                  String[] files; // List of names of files in the directory.
      System.out.println(indent + "Directory \"" + dir.getName() + "\":");
       indent += " "; // Increase the indentation for listing the contents.
       files = dir.list();
       for (int i = 0; i < files.length; i++)
       {
            // If the file is a directory, list its contents
            // recursively. Otherwise, just print its name.
           File f = new File(dir, files[i]);
           if (f.isDirectory())
               listContents(f, indent);
           else
            System.out.println(indent + files[i]); }
  } // end listContents()


} // end class RecuDirList
0
 

Author Comment

by:nickmcdonald
ID: 11796276
THANKS A LOT, I GOT IT.

Nick
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question