Question

How do I correct this error in my code?

Task List X fileproject - FileProject/src/FileProject.java - Eclipse IDE HOLA COME BEI Package Explorer X FileProject.java X

When it got to the file type I can only go "null" from a certain point and I'm wondering how to correct this error.
Also can I get a word document on this coding strategy for this problem? How much of this am I doing correctly?

The original problem description:

Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is a file or a directory and list the size for each file. The program should print its output to System.out.

  • Embed the program in a Microsoft Word document with a description of your programming strategy.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

FIXED JAVA CODE:

import java.io.File;
public class Main {
    public static void main(String[] args) {
        File folderName = new File(args[0]);// get directory name from command line arguments
        // check if folderName is really a directory or not
        if (!folderName.isDirectory()) {
            System.out.println(args[0] + " is not a directory");
            System.exit(1);// quit the program
        }
        File[] files = folderName.listFiles();
        String filetype = "";
        System.out.println("Name \t\t file/directory \t file size (if it is file)");
        // Loop through all files in the folder
        for (File file : files) {
            // If it is a directory
            if (file.isDirectory()) {
                filetype = "directory";
                System.out.println(file.getName() + "\t\t" + filetype);
            }
            // If it is a file
            if (file.isFile()) {
                filetype = "file";
                System.out.println(file.getName() + "\t\t" + filetype + "\t\t" + file.length());
            }
        }
    }
}

SAMPLE OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
How do I correct this error in my code? When it got to the file type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Reading and Writing Files in a Directory Create a program that provides a listing of all...

    Reading and Writing Files in a Directory Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is...

  • Using listAllFiles as a guide, write a method that has one File argument: if the argument...

    Using listAllFiles as a guide, write a method that has one File argument: if the argument is a directory, the method returns the total number of files below it. If the argument represents a file, the method just returns 1. public static int countFiles(File f)   import java.io.File; public class FileLister { public static void main(String[] args) { // Choose the directory you want to list. // If running in Eclipse, "." will just be the current project directory. // Use...

  • what is wrong with my code. what should I do with this kind of error 61...

    what is wrong with my code. what should I do with this kind of error 61 QueueDemo java - Files - RaProjects/one- A Queue Demoava Queueinterface Java inport java.util.LinkedList; import java.util.Queue public class QueueDemo public static void main(String[] args) { 10 11 12 13 Queue String myQueue new LinkedlistString();//Create a reference to a queue Interface myqueue.add("A");//Call the enqueue method on myQueue passing the string value of "A" myQueue.add("B");//call the enqueue method on nyQueue passing the String value of "3" myQueue.add("C");//Call...

  • I need help in this basic java programming. I just need a program that output this...

    I need help in this basic java programming. I just need a program that output this according to the original picture above. Quick Access 熅Package Explorer 23 曰ちい▽ーロ D Volunteer,java D Volunteer Test.java 23 -ロ 貝Task List 2 public class VolunteerTest ▼申(default package) Volunteer java 4 public static void main(String[] args) Find THIS CODE MUST NOT BE CHANGED IN ANY WAY!1 JRE System Library [JavaSE-18] // The code below will be used to test the Volunteer class I/ created by...

  • I have one method {search()} that search for a particular file in a directory that the filename s...

    I have one method {search()} that search for a particular file in a directory that the filename start by "B" and it will return the files. I have 2 file that start by B [Bcc.txt, Ba.txt]. But in main method I want to count a word "light" how many times it appeares in the each files [Bcc.txt, Ba.txt]. However, I really don't know how to do it. I need help on that. public class CountWord {    public static void main(String[]...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • Information About This Project             In the realm of database processing, a flat file is a...

    Information About This Project             In the realm of database processing, a flat file is a text file that holds a table of records.             Here is the data file that is used in this project. The data is converted to comma    separated values ( CSV ) to allow easy reading into an array.                         Table: Consultants ID LName Fee Specialty 101 Roberts 3500 Media 102 Peters 2700 Accounting 103 Paul 1600 Media 104 Michael 2300 Web Design...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT