java - 大批- 获取单词在行号上的字符位置
问题描述
我有一个 arrayList ,它保存了我读入的文件的内容。我只需要帮助弄清楚让它在每一行显示该行上每个单词的位置。(稍后我将编写一个忽略 // 和其后所有内容的函数。
所需的输出:第 1 行,字符/位置 1,程序第 1 行,位置 2,二十:第 2 行,位置 3,int 等等..
当前输出读取:行号:0 行读取://此程序应打印数字 20。行号:1 行读取:程序二十:行号:2 行读取:int a; 行号:3行读取:int b;
代码:
public class Lex_functions {
ArrayList < String > list = new ArrayList < String > ();
int lineNum = 0;
int charNum = 0;
int totalLines = 0;
void readFile() throws FileNotFoundException {
File f = new File("ab.txt");
FileReader fr = null;
try {
fr = new FileReader(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
BufferedReader infile = new BufferedReader(fr);
String line = "";
boolean done = false;
try {
while (!done) {
line = infile.readLine();
if (line == null) {
done = true;
} else {
list.add(line);
}
}
infile.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
for (String item: list) {
//System.out.println(item);
while (lineNum <= (list.size() - 1)) {
System.out.println("Line Number: " + lineNum + " " + "line reads: " + " " + list.get(lineNum));
// System.out.println(list.size());
lineNum++;
charNum++;
//System.out.println();
}
// System.out.println(list.indexOf(4));
//System.out.println(list[0]);
// System.out.println("Index of Program: "+list.get(2));
} //end readfelil
}
}
解决方案
因此,我不确定您要做什么,因为您显示所需输出的方式不可读,并且您没有向我们提供要显示的文件内容。但如果我理解得很好,如果你有以下数组:
{“你好世界”},{“祝你有美好的一天”}
您要显示的内容应该是:
- 字:你好,行号:0,字号0
- 字:世界,行号:0,字号1
- 字:有,行号:1,字号0
- 字:a,行号:1,字号1
- 字:好,行号:1,字号2
- 字:天,行号:1,字号3
因此,首先有一种简单的方法可以将文件的所有行读入 Array List :
/**
* Read a file and put all its content into a String List
* @param fileName file path
* @return
*/
public static ArrayList<String> readFileInList(String fileName){
List<String> lines = Collections.emptyList();
try {
lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
} catch (IOException e) {
// do something
e.printStackTrace();
}
return (ArrayList<String>) lines;
}
接下来是你循环的方式......
for (String item: list) {
//System.out.println(item);
while (lineNum <= (list.size() - 1)) {
System.out.println("Line Number: " + lineNum + " " + "line reads: " + " " + list.get(lineNum));
// System.out.println(list.size());
lineNum++;
charNum++;
}
}
你所做的基本上是重叠 2 个循环通过相同的项目。事实是字符串是一个字符列表,所以你想要做的是遍历你的字符串。一个字符串也可以用正则表达式分成子字符串,创建一个单词列表:
/**
* Parse the string (the line) into words. All spaces and dots are kept.
* <br/> The following string
* <ul>
* <li>"Hello, my name is Blanc! I'm a student. Can you help me? Thanks."</li>
* </ul>
* will be parsed as :
* <br/> "|Hello, |my |name |is |Blanc!| |I'm |a |student.| |Can |you |help |me?| |Thanks.|"
* @param input the string to parse
* @return the words of the input as an array
* @see String#split(String)
*/
public static String[] parseInputToWordsArray(String input) {
return input.split("(?<= )|(?<=\\.)|(?<=\\?)|(?<=!)");
}
接下来,您要检查数组的行及其单词列表以打印输出(未测试但应该可以):
ArrayList <String> list = new ArrayList();
int lineNum;
for (lineNum = 0; lineNum<list.size(); lineNum++) {
int wordNum;
String[] word = parseInputToWordsArray(list.get(lineNum));
for(wordNum = 0; wordNum < word.length; wordNum++){
System.out.println("Word : "+word[wordNum]+", Line Number : " + lineNum + ", Word Number: " + " " + wordNum);
}
}
希望它可以帮助
推荐阅读
- node.js - 有没有办法在聚合查询中的 $and 运算符中使用多个正则表达式?
- c# - ModelState 错误不会以部分视图形式显示
- javascript - 如何使用 Dexie.js 删除值字段内的键/值?
- python - 使用单个 os.mkdir 函数(Python)生成多个目录
- spring-boot - kafka 重启后 kubernetes 上的“分区有没有匹配侦听器的领导代理”
- r - 在 r 中绘制 Black Scholes 期权价格
- docker - 如何使用 jenkins 管道参数标记 docker 图像?
- cobol - 如何编写比 COBOL 中的最大 LRECL 更长的输出?
- ios - dyld:当应用程序在 iOS 12 上使用 @available(iOS 13.0, *) 运行时,库未加载 SwiftUI
- php - MySQL 和 MSSQL 中用于 IF INSERT ELSE UPDATE 的通用语法