Solved

Please help me to compile this Java Program !!!

Posted on 2004-08-11
5
729 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now