每次当前行包含一些特定关键字时,我的程序都必须使用数组或任何正则表达式来缩进文本文件中的文本。
示例;
start
write "enter your name
read name
write "enter your age"
read age
if age==100 then
do
print "you stink"
while age !=100
end if
while something
if something
do something
while something
end if
end
应该是:
start
write "enter your name"
read name
write "enter your age"
read age
if age==100 then
do
print "you stink"
while age !=100
end if
while something
if something
do something
while something
end if
end
我无法让我的程序正确缩进第一个“关键字”之后的任何内容。关键字为“while、if、do、end if、end、start 等...”
我不允许使用数组或任何正则表达式
我的代码读取文本文件的部分:
String corrIndent (String pseudocode2) {
String sTrim = ""; //pseudocode to return
String sTmp;
int debut = 0; //current line start index
int fin; //current line end index
fin = pseudocode2.indexOf("\n", debut);//read lines
do {
//extracts current line
sTmp = pseudocode2.substring(debut, fin);
//opens the do..while loop indent level
if(sTmp.contains("do")){//opens do indent(indents 1 level)
for (int i=1;i<=999;i++){
sTmp =pseudocode2.substring(debut, fin);
//sTrim="\t";
if (sTmp.contains("while")){//close do indent (reduce indentation 1 level)
i=999;//supposes that the text file is never that long
}
//add current line to string to return
//with a newline.
sTrim = sTrim + sTmp+ ""+" ";
//ajuster le debut de la ligne courante suivante
debut = fin + 1;
//trouver la fin de la ligne courante suivante
fin = pseudocode2.indexOf("\n", debut);
}
}
{//when no more indentation is needed
sTrim = sTrim + sTmp + "\n";
debut = fin + 1;
fin = pseudocode2.indexOf("\n", debut);
}
}while (fin != -1);
return sTrim;
}
使用此代码,我可以正确缩进 do block ,但如果该 do block 内有另一个关键字,它将不会获得新的缩进级别。
这是为什么?
我尝试了多种方法将缩进级别保存为变量,但没有成功。
最佳答案
This solution uses both arrays and regex - I am posting here for future coders that may not have that restriction.
这是我刚刚制作的一个快速示例。它允许您设置要缩进的单词数组以及哪些单词应触发缩进减少。
还有一些改进的空间,因为它无法捕获您可能需要的所有情况。
您还需要将输出写入文件。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
final String IN_FILE = "test.txt";
final String OUT_FILE = "sample/indent/result.txt";
final int INDENT_SPACES = 4; // Number of spaces to indent
StringBuilder output = new StringBuilder();
int currentIndentLevel = 0;
// Increase indent level on these keywords
String[] keywordsIn = {"start", "if", "do"};
// Decrease indent level on these keywords
String[] keywordsOut = {"while", "end"};
// Read the file, line by line
try {
BufferedReader reader = new BufferedReader(new FileReader(IN_FILE));
String line = reader.readLine();
while (line != null) {
// Indent the line based on current level
for (int i = 0; i < INDENT_SPACES * currentIndentLevel; i++) {
output.append(" ");
}
output.append(line).append("\n");
// Check if it contains a keyword
for (int i = 0; i < keywordsIn.length; i++) {
// Line begins with a keyword to increase the indent
if (line.matches(keywordsIn[i] + "\\b.*")) {
currentIndentLevel += 1; // Increase the indent level
}
}
for (int i = 0; i < keywordsOut.length; i++) {
// Line begins with a keyword to decrease the indent
if (line.matches(keywordsOut[i] + "\\b.*")) {
currentIndentLevel -= 1; // Decrease the indent level
}
}
// Get next line
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(output.toString());
}
}
采用原始输入文件,这就是该程序的输出:
start
write "enter your name
read name
write "enter your age"
read age
if age==100 then
do
print "you stink"
while age !=100
end if
while something
if something
do something
while something
end if
end
关于java - 如何在不使用数组或任何正则表达式的情况下缩进包含特殊关键字的文本文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073378/