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);