OK, so i do have a perfectly good working code but im trying a different way and its bugging me why its not working. any help would be much appreciated. the program suppose to list all the available directories and then ask the user which directory he's searching for, takes the input and search for the directory. if the directory exist it will find it and list all the files in it and the path to it. if it can't find the directory it will throw a message or exception saying so.

Now it lists all the directories just fine and asks the user input but it wont go past that and thats where i'm stucked......

here's the code:

import java.io.File;
import java.util.Scanner;

import javax.swing.filechooser.*;

public class MyMain {

    public static void main(String[] args) {

        //String dir;
        String keyWord = "";
        Scanner keyboard = new Scanner(System.in);
        File[] dir = File.listRoots();
        //System.out.println("Please enter the key to root directory from the list below: "  
        /* FileSystemView.getFileSystemView().getHomeDirectory() + "     "  +  FileSystemView.getFileSystemView().getRoots()[0] + "   "  +
                System.getProperty("user.dir") + File.listRoots()  */;

        System.out.println("Available root directories in filesystem are :  ");
         for(int i=0 ; i < dir.length ; i++)

         keyWord = keyboard.nextLine();

         System.out.println(" Please enter the directory to scan: ");
         String path = keyboard.nextLine();

        while(path != null){

                if( rootDirectories.exists() == false){
                    System.out.println("Error: That directory does not exist. ");

                if (rootDirectories.canRead() = false){
                    System.out.println("Error, cannot read that directory. ");

                System.out.println("Directory is Valid. "); //test output



I know its incorrect but can someone give me an idea on how to correct it. thanks

Edited by pritaeas: Moved to software dev.

1 Year
Discussion Span
Last Post by Taywin

what exactly is it you are unclear about? First you say you have "perfectly good working code", later, you say "I know its incorrect".
Either it is perfeclty good working, or it is incorrect.

What is incorrect about it? Do you get an exception? is it compile/ runtime? do you get unexpected results?
try to be as specific as you can.


I don't know if that is the source for your problem but here:

 if (rootDirectories.canRead() = false){
                    System.out.println("Error, cannot read that directory. ");

you put just one "=" sign instead of two ^^


Your program won't get out of a loop if the path variable is not null...

Anytime you are using a while or do-while loop, ensure that you can GET OUT of the loop in some way or you will be stuck inside an infinite loop...

Anyway, the idea is that you need to use either BFS or DFS algorithm in your search. Pick one. Also, you need to be clear if you are looking for "case-sensitive" or "case-insensitive" name. Another decision to make is that what you would do when there are 2 or more exactly same directory names in your whole directory tree?

Edited by Taywin

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.