- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我首先要说的是,我已经查看了名为“我必须制作一个循环以获取用户输入,直到输入“完成””的线程,我对那里给出的代码答案没有运气。
我对编辑命令的描述是这样的:
"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."
现在我的代码是这样的:
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
声明打印。我的else
声明代码是这样的:
else {
System.out.println("That is not a command. Please try again.");
}
同样有趣的是,该程序贯穿了整个 do while loop
然后打印其他。我知道这一点是因为确切打印的是:$That is not a commond. Please try again.
这是我的do while loop
的开始:
do {
System.out.print("$");
String input = s.nextLine();
input = input.toLowerCase();
spaceSplit = input.split(" ");
相当困惑。还有我的edit(String name, String content)
功能如下:
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());
}
}
}
}
}
如您所见,我已在 edit(name,content)
的末尾进行了检查。方法通过打印文本文件的父目录来检查文件是否正确创建。
这就是我调用编辑命令后我的程序应该如何运行:
$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!
如果您提供任何帮助,我们将不胜感激。
这是全部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);
最佳答案
好吧,我想我会在这里回答我自己的问题。现在一切都很完美。
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);
}
}
}
关于java - 接受用户输入,直到输入 <<EOF>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34167042/
读取文件时,我知道提供的最后一个字符是 EOF。现在,当我有 EOF 时会发生什么?该文件中的字符? 如何区分文件的“真实”结尾和 EOF特点? 最佳答案 我决定将我的评论移至答案。 您的文件中不能有
GNU Bash - 3.6.6 Here Documents [n]<<[-]word here-document delimiter If any part of word is
试图查看论坛但无法重新访问。 我正在尝试阅读文本。文字是:“给 b” 但是在使用 fgetc() 时,没有达到 EOF,最后我得到 '\n',然后是无穷大的 'y' 样本。 这是我的代码: Node*
我正在做一个非常简单的作业,需要输出文件的内容,但在做任何事情之前我意外地到达了 EOF。该文件仅包含单词“pig”,并且由于某种原因 EOF 返回 16。我正在使用 Dev-Cpp 并且该程序是用
我的 kubectl 无法读取文件,每次都返回 'error: unexpected EOF'。 kubectl apply -f service.yaml > error: unexpected E
我想知道什么时候使用 cat Hello world! () { setopt localoptions shnullcmd test-nullcmd } # nothing will be
我试图找出我正在向其中写入特定数据的文件中不需要的尾随结束数据的原因,并且不相信我在写入文件时出错。 输出如下: building room_numbr capacity packard
考虑下面的简单例子 #include #include #include using namespace std; int main() { string str = "string";
我在一个程序中有这个片段(在 Visual Studio 2005 中): if(_eof(fp->_file)) { break; } 当达到 eof 时,它打破了封闭循环。但是程序无法解析
我正在尝试为 Typed Racket 中的以下函数定义类型注释: (define (neof x) (if (eof-object? x) #f x)) 不加注释给出了类型: (Any ->
我正在为 Linux 构建系统的模块编写 .spec 文件,遇到一个小问题并想分享它。 用于编写脚本文件: cat /path/to/somewhere/script #blah blah EOF
我有一个 C++ 程序,它从一个文件中读取我希望有一堆格式相同的记录。如果遇到意外情况,无论是记录格式不正确还是输入失败,我都想停止阅读,我想区分这些不同的情况。 我看过 this answer并查看
我注意到它们之间的几个区别: 在 <
预期我正在生成子进程并执行“ssh”远程框。从最近几天开始它工作正常,现在突然间,每当我尝试生成子进程时,它都会抛出错误。不确定发生了什么。 直到现在我一直在使用 pexpect 3.1,我遇到了这个
这个问题已经有答案了: Why two EOF needed as input? [duplicate] (2 个回答) Why do I require multiple EOF (CTRL+Z)
我正在浏览一个文件来寻找特定的词 Char[50]=getline(file,/n) 使用 getline 将每一行存储到一个 char 数组中以与我要查找的字符串进行比较 If( “”==char[
引用两个问题: Incorrect output from C++ Primer 1.4.4 Confused by control flow execution in C++ Primer exam
我刚刚在 this 中找到一条评论回答说在循环条件中使用 iostream::eof “几乎肯定是错误的”。我通常使用类似 while(cin>>n) 的东西——我猜它会隐式检查 EOF。 为什么使用
我刚刚在 this 中找到一条评论回答说在循环条件中使用 iostream::eof “几乎肯定是错误的”。我通常使用类似 while(cin>>n) 的东西——我猜它会隐式检查 EOF。 为什么使用
我刚刚在 this 中找到一条评论回答说在循环条件中使用 iostream::eof “几乎肯定是错误的”。我通常使用类似 while(cin>>n) 的东西——我猜它会隐式检查 EOF。 为什么使用
我是一名优秀的程序员,十分优秀!