1

I'm going to start off by saying I already looked at the thread called "I have to make a loop taking users input until "done" is entered" I had no luck with the code answers that were given there.

The description I've been given for my edit command is this:

"Edits a text file if exists. Otherwise, creates new text file. The command waits for the user to type in text(must support multiple lines of text). The user ends the input by typing <<EOF>> and hitting enter."

Right now the code I have is this:

else if (spaceSplit[0].equals("edit")) {
            String name = spaceSplit[1];
            boolean endOfFile = false;
            String content = "";

            while(endOfFile == false){
                String userInput = s.next();
                content += userInput;
                if(content.contains("<<EOF>>")){
                    endOfFile = true;
                }
            }

            FileSystem.edit(name, content);


        }

Nothing errors-out, but my else statement prints. My else statement code is this:

else {
            System.out.println("That is not a command. Please try again.");
        }

What is also funky is that the program goes through the whole do while loop then prints the else. I know this because what is exactly printed is: $That is not a commond. Please try again.

Here is the beginning of my do while loop:

do {

        System.out.print("$");

        String input = s.nextLine();
        input = input.toLowerCase();
        spaceSplit = input.split(" ");

Quite confusing. Also my edit(String name, String content) function is as follows:

public static void edit(String name, String content){
    for(int i = 0; i < texts.size(); i++){
        if(texts.get(i).getName().equals(name)){
            texts.get(i).setContent(content);
        } else {
            texts.add(new TextFile(name,content));

            for(int j = 0; j < directories.size(); j++){
                if(directories.get(j).getName().equals(wDir.getName())){
                    texts.get(texts.size() - 1).setParent(directories.get(j));
                    System.out.println("The parent of " + name + " is " + directories.get(j).getName());
                }
            }
        }
    }
}

As you can see I've done a check at the end of my edit(name,content) method to check if the file is correctly created by printing out the parent directory of the text file.

This is how my program should function once I call the edit command:

$mkdir d
$cd d
$edit stuff.txt
Hello everyone, this is just an example!<<EOF>>
The parent of stuff.txt is d
$exit
Good Bye!

Any help provided would be greatly appreciated.

Here is the whole do while loop:

do {

        System.out.print("$");

        String input = s.nextLine();
        input = input.toLowerCase();
        spaceSplit = input.split(" ");

        if (spaceSplit[0].equals("mkdir")) {
            if (spaceSplit[1].equals("-p")) {
                for (int i = 3; i < spaceSplit.length; i++) {

                }
            } else if (spaceSplit[1].contains("/")){
                    //This method will create a directory farther down the tree like creating c in a/b/c
                    String[] dirSplit = spaceSplit[1].split("/");
                    int length = dirSplit.length;

                    FileSystem.mkdir(dirSplit[length-1]);
                    int directoriesLength = FileSystem.directories.size();

                    for(int i = 0; i < FileSystem.directories.size(); i++){
                        if(dirSplit[length-2].equals(FileSystem.directories.get(i))){
                            FileSystem.directories.get(i).addChild(FileSystem.directories.get(directoriesLength-1));
                            //Checking if this works
                            System.out.println("The child was created succesfully");
                        }
                    }

            } else {
                for (int i = 1; i < spaceSplit.length; i++) {
                    FileSystem.mkdir(spaceSplit[i]);
                }
            }

        } else if (spaceSplit[0].equals("cd")) {
            FileSystem.cd(spaceSplit[1]);
        } else if (spaceSplit[0].equals("pwd")) {
            FileSystem.pwd();
        } else if (spaceSplit[0].equals("ls")) {

        } else if (spaceSplit[0].equals("edit")) {
            String name = spaceSplit[1];
            boolean endOfFile = false;
            String content = "";

            while(endOfFile == false){
                String userInput = s.next();
                content += userInput;
                if(content.contains("<<EOF>>")){
                    endOfFile = true;
                }
            }

            FileSystem.edit(name, content);


        } else if (spaceSplit[0].equals("cat")) {
            for(int i = 1; i < spaceSplit.length; i++){
                FileSystem.cat(spaceSplit[i]);
            }
        } else if (spaceSplit[0].equals("updatedb")) {

        } else if (spaceSplit[0].equals("locate")) {

        } else if (spaceSplit[0].equals("exit")) {
                exitProg = true;
                System.out.println("Good bye!");
        } else {
            System.out.println("That is not a command. Please try again.");
        }

    } while (exitProg == false);
CSGuy94
  • 55
  • 7
  • 1
    Please show the entire do while loop as a single block. Do not show sections out of order. – Jim Garrison Dec 08 '15 at 22:18
  • Where are you getting user input from? Also, how do you plan on running this code from the command line? You can't just say "edit stuff.txt" and have it run some jar and perform operations – user1231232141214124 Dec 08 '15 at 22:20
  • I see you have a for loop thats doing nothing, that should not be there – Manish Mallavarapu Dec 08 '15 at 22:27
  • I apologize @JimGarrison I've posted the whole `do while loop` at the bottom of the main post. And @redFIVE the `do while loop` should help you make sense of it as well. I'm doing a fake unix program basically. The `edit` command is recognized and then the next input is taken as the name of the file I will be creating or altering the content to. – CSGuy94 Dec 08 '15 at 22:29
  • I have not finished constructing the whole program yet. The two loops that are `else if (spaceSplit[0].equals("updatedb"))` and `else if(spaceSplit[0].equals("locate"))` Use a binary search tree which I have not figured out yet. @ManishMallavarapu – CSGuy94 Dec 08 '15 at 22:30
  • There is an awful lot wrong with this code. The best thing you can do is run it in a debugger (Eclipse, Intellij Idea, NetBeans, etc) and step through it one line at a time. This will help you learn what the code does, and where your mental model of the code does not match reality. Pointing out all the problems is really beyond the scope of an SO answer. – Jim Garrison Dec 08 '15 at 22:33
  • Really? I'm running the code in Eclipse currently. I have yet to test my whole `mkdir` method. And I know for a fact the `pwd` method has an error in it. I'll figure out the rest eventually, it would help if I could figure out how to use my `edit` method correctly first though. @JimGarrison – CSGuy94 Dec 08 '15 at 22:38

1 Answers1

0

Alright, well I guess I'll answer my own question here. Everything works perfectly now.

else if (spaceSplit[0].equals("edit")) {
            if(spaceSplit.length > 1) {
                String name = spaceSplit[1];
                boolean endOfFile = false;
                String content = "";

                while (!(content.contains("<<EOF>>"))) {
                    String userInput = s.nextLine();
                    content += userInput + " ";
                }

                String end = "<<EOF>>";

                content = content.replace(end, "");

                int size = tree.getTexts().size();

                if (size != 0) {
                    for (int i = 0; i < size; i++) {
                        if (tree.getTexts().get(i).getName().equals(name)) {
                            tree.getTexts().get(i).setContent(content);
                        } 
                    }
                    tree.edit(name, content);
                } else {
                    tree.edit(name, content);
                }
            }

        }
CSGuy94
  • 55
  • 7