gpt4 book ai didi

java - 在 .txt 文件中搜索字符串并在 Java 中获取行号和列号

转载 作者:行者123 更新时间:2023-12-03 18:41:14 26 4
gpt4 key购买 nike

我目前遇到了一个问题。我应该编写一个程序,能够在作为参数给出的 .txt 文件中搜索字符串。程序必须返回找到的字符串的行和列。我正在努力寻找一种方法来实现这一目标,但不知道如何继续下去。我会很高兴收到你的来信。

这是我处理任务的尝试:- 我考虑过通过缓冲读取器将文件内容保存在字符串数组中,但这似乎不起作用,因为我无法从一开始就定义数组的长度- 我还考虑过通过缓冲阅读器将文件内容保存在一个字符串中,然后将这个字符串拆分为字符。但是,我不确定那时我将如何检索原始文件中的行。

这是我目前的非功能代码:

public class StringSearch{
public static void main(String[] args){
if(args.length > 0){
BufferedReader br = null;
String text = null;
try{
br = new BufferedReader(new FileReader(args[0]));
// attempt of saving the content of the "argument" file in a string array and then in a string
String[] lines = new String[]; // I know this does not work like this
for( int i = 0; i < lines.length; i++){
lines[i] = br.readLine;
text = text + lines[i];
i++;
}
text.split("\r\n");

} catch (IOException ioe){
ioe.printStackTrace();
} finally{
if (br != null) {
try{
br.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}


}

}
}
}

最佳答案

您可以按如下方式进行:

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
if (args.length != 2) {
System.out.println("The correct syntax to use this program is: java Main <filename.txt> <text-to-search>");
return;
}
Scanner scanner;
File file = new File(args[0]);
int rowCount = 1, index;
String line;

// Map to collect row and col info of the search string
Map<String, String> lineColMap = new HashMap<String, String>();

if (!file.exists()) {
System.out.println("The file, " + args[0] + " does not exist");
return;
}
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()) {// Loop until the last line in the file
line = scanner.nextLine();// Read a line from the file
index = line.indexOf(args[1]);// Find if the string exists in the line
if (index != -1) {// If the string exists
// Put the row and col info of the search string into the map
lineColMap.put("Row: " + rowCount, "Column: " + index);
}
rowCount++;// Increase the row count
}
} catch (Exception e) {
System.out.println("Error occured while processing the file");
e.printStackTrace();
}
if (lineColMap.entrySet().size() > 0) {// If there is at least one entry collected into the map
System.out.println("'" + args[1] + "' exists in " + args[0] + " as follows:");
for (Map.Entry<String, String> entry : lineColMap.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
} else {
System.out.println("'" + args[1] + "' does not exist in " + args[0]);
}
}
}

示例运行: java Main input.txt of

'of' exists in input.txt as follows:
Row: 1, Column: 51
Row: 2, Column: 50
Row: 3, Column: 50
Row: 5, Column: 71

input.txt内容如下:

Stack Overflow is a question and answer site for professional and enthusiast programmers.
It is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky.
It features questions and answers on a wide range of topics in computer programming.
It was created to be a more open alternative to earlier question and answer sites such as Experts-Exchange.
The name for the website was chosen by voting in April 2008 by readers of Coding Horror, Atwood's popular programming blog.

代码逻辑很通俗,相信第一次看的时候应该能看懂。如有任何疑问,请随时发表评论。

关于java - 在 .txt 文件中搜索字符串并在 Java 中获取行号和列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60016947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com