Search code examples
javabluej

How to proceed with making an in - BlueJ Java file manager


I know that it is a bit of a noob program, but I'm slowly getting confused. The 'down' function is going to act like cd and the 'up' function would act like cd..

I have no clue how to allow the user to create a file or folder. I attempted using arrayLists instead of arrays but couldn't sort out the errors. Any help would be appreciated.

    import java.util.Scanner;
    class FileManager {
    //array of arrays will go here
    String Dir[] = {"UserOne"};
    String SystemFolders [] = {"Documents","","",};
    String SubFiles [] = {"","","","","",""};
    String Nav [][] = { Dir, SystemFolders, SubFiles};
    int levelCounter = 0;
    public void main(String[]args)   {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter a command");
        String command = sc.next();
            if (command.compareTo("down") == 0)
                down();
            //else if is on the way
    }
    void down ()    {
        //This will execute when the command is 'down'
        System.out.println(Nav[++levelCounter]);
    }
    void up ()  {
        //This will execute when the command is 'up'. It acts like cd..
        System.out.println(Nav[--levelCounter]);
    }
}

Solution

  • If this is the entry point of your program then you need to declare your main method as static, like so

    public static void main(String[] args)
    

    Then to access the methods in your FileManager class in your main method you need to create an instance of your class in your main method. Like this

    public static void main(String[]args)   {
        FileManager fm = new FileManager();  // Creates an instance
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter a command");
        String command = sc.next();
        if (command.equals("down")) // equals will suffice in this case
                                    // or equalsIgnoreCase() if you dont want case to be a problem
            fm.down(); // Notice now this calls the down method from the instance
        //else if is on the way
    }
    

    Then look at this to example to create files or this to create folders