[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Please help me to compile this Java Program !!!

Posted on 2004-08-11
5
Medium Priority
?
817 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
3 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 400 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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 …
Loops Section Overview

867 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